3 * WordPress Translation API
10 * Gets the current locale.
12 * If the locale is set, then it will filter the locale in the 'locale' filter
13 * hook and return the value.
15 * If the locale is not set already, then the WPLANG constant is used if it is
16 * defined. Then it is filtered through the 'locale' filter hook and the value
17 * for the locale global set and the locale is returned.
19 * The process to get the locale should only be done once but the locale will
20 * always be filtered using the 'locale' hook.
23 * @uses apply_filters() Calls 'locale' hook on locale value.
24 * @uses $locale Gets the locale stored in the global.
26 * @return string The locale of the blog or from the 'locale' hook.
28 function get_locale() {
32 return apply_filters( 'locale', $locale );
34 // WPLANG is defined in wp-config.
35 if (defined('WPLANG'))
41 $locale = apply_filters('locale', $locale);
47 * Retrieve the translated text.
49 * If the domain is set in the $l10n global, then the text is run through the
50 * domain's translate method. After it is passed to the 'gettext' filter hook,
51 * along with the untranslated text as the second parameter.
53 * If the domain is not set, the $text is just returned.
56 * @uses $l10n Gets list of domain translated string (gettext_reader) objects.
57 * @uses apply_filters() Calls 'gettext' on domain translated text
58 * with the untranslated text as second parameter.
60 * @param string $text Text to translate.
61 * @param string $domain Domain to retrieve the translated text.
62 * @return string Translated text
64 function translate($text, $domain = 'default') {
67 if (isset($l10n[$domain]))
68 return apply_filters('gettext', $l10n[$domain]->translate($text), $text, $domain);
70 return apply_filters('gettext', $text, $text, $domain);
73 function before_last_bar( $string ) {
74 $last_bar = strrpos( $string, '|' );
75 if ( false == $last_bar )
78 return substr( $string, 0, $last_bar );
82 * Retrieve the translated text and strip context.
84 * If the domain is set in the $l10n global, then the text is run through the
85 * domain's translate method. After it is passed to the 'gettext' filter hook,
86 * along with the untranslated text as the second parameter.
88 * If the domain is not set, the $text is just returned.
93 * @param string $text Text to translate
94 * @param string $domain Domain to retrieve the translated text
95 * @return string Translated text
97 function translate_with_context( $text, $domain = 'default' ) {
98 return before_last_bar( translate( $text, $domain ) );
103 * Retrieves the translated string from the translate().
105 * @see translate() An alias of translate()
108 * @param string $text Text to translate
109 * @param string $domain Optional. Domain to retrieve the translated text
110 * @return string Translated text
112 function __($text, $domain = 'default') {
113 return translate($text, $domain);
117 * Displays the returned translated text from translate().
119 * @see translate() Echos returned translate() string
122 * @param string $text Text to translate
123 * @param string $domain Optional. Domain to retrieve the translated text
125 function _e($text, $domain = 'default') {
126 echo translate($text, $domain);
130 * Retrieve context translated string.
132 * Quite a few times, there will be collisions with similar translatable text
133 * found in more than two places but with different translated context.
135 * In order to use the separate contexts, the _c() function is used and the
136 * translatable string uses a pipe ('|') which has the context the string is in.
138 * When the translated string is returned, it is everything before the pipe, not
139 * including the pipe character. If there is no pipe in the translated text then
140 * everything is returned.
144 * @param string $text Text to translate
145 * @param string $domain Optional. Domain to retrieve the translated text
146 * @return string Translated context string without pipe
148 function _c($text, $domain = 'default') {
149 return translate_with_context($text, $domain);
153 * Retrieve the plural or single form based on the amount.
155 * If the domain is not set in the $l10n list, then a comparsion will be made
156 * and either $plural or $single parameters returned.
158 * If the domain does exist, then the parameters $single, $plural, and $number
159 * will first be passed to the domain's ngettext method. Then it will be passed
160 * to the 'ngettext' filter hook along with the same parameters. The expected
161 * type will be a string.
164 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
165 * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
166 * along with $single, $plural, and $number parameters. Expected to return string.
168 * @param string $single The text that will be used if $number is 1
169 * @param string $plural The text that will be used if $number is not 1
170 * @param int $number The number to compare against to use either $single or $plural
171 * @param string $domain Optional. The domain identifier the text should be retrieved in
172 * @return string Either $single or $plural translated text
174 function __ngettext($single, $plural, $number, $domain = 'default') {
177 if (isset($l10n[$domain])) {
178 return apply_filters('ngettext', $l10n[$domain]->ngettext($single, $plural, $number), $single, $plural, $number);
188 * @see __ngettext() An alias of __ngettext
192 $args = func_get_args();
193 return call_user_func_array('__ngettext', $args);
197 * @see _n() A version of _n(), which supports contexts --
198 * strips everything from the translation after the last bar
201 function _nc( $single, $plural, $number, $domain = 'default' ) {
202 return before_last_bar( __ngettext( $single, $plural, $number, $domain ) );
206 * Register plural strings in POT file, but don't translate them.
208 * Used when you want do keep structures with translatable plural strings and
213 * 'post' => ngettext_noop('%s post', '%s posts'),
214 * 'page' => ngettext_noop('%s pages', '%s pages')
217 * $message = $messages[$type];
218 * $usable_text = sprintf(__ngettext($message[0], $message[1], $count), $count);
221 * @param $single Single form to be i18ned
222 * @param $plural Plural form to be i18ned
223 * @param $number Not used, here for compatibility with __ngettext, optional
224 * @param $domain Not used, here for compatibility with __ngettext, optional
225 * @return array array($single, $plural)
227 function __ngettext_noop($single, $plural, $number=1, $domain = 'default') {
228 return array($single, $plural);
232 * @see __ngettext_noop() An alias of __ngettext_noop()
236 $args = func_get_args();
237 return call_user_func_array('__ngettext_noop', $args);
241 * Loads MO file into the list of domains.
243 * If the domain already exists, the inclusion will fail. If the MO file is not
244 * readable, the inclusion will fail.
246 * On success, the mofile will be placed in the $l10n global by $domain and will
247 * be an gettext_reader object.
250 * @uses $l10n Gets list of domain translated string (gettext_reader) objects
251 * @uses CacheFileReader Reads the MO file
252 * @uses gettext_reader Allows for retrieving translated strings
254 * @param string $domain Unique identifier for retrieving translated strings
255 * @param string $mofile Path to the .mo file
256 * @return null On failure returns null and also on success returns nothing.
258 function load_textdomain($domain, $mofile) {
261 if ( is_readable($mofile))
262 $input = new CachedFileReader($mofile);
266 $gettext = new gettext_reader($input);
268 if (isset($l10n[$domain])) {
269 $l10n[$domain]->load_tables();
270 $gettext->load_tables();
271 $l10n[$domain]->cache_translations = array_merge($gettext->cache_translations, $l10n[$domain]->cache_translations);
273 $l10n[$domain] = $gettext;
275 unset($input, $gettext);
279 * Loads default translated strings based on locale.
281 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
282 * translated (.mo) file is named based off of the locale.
286 function load_default_textdomain() {
287 $locale = get_locale();
289 $mofile = WP_LANG_DIR . "/$locale.mo";
291 load_textdomain('default', $mofile);
295 * Loads the plugin's translated strings.
297 * If the path is not given then it will be the root of the plugin directory.
298 * The .mo file should be named based on the domain with a dash followed by a
299 * dash, and then the locale exactly.
303 * @param string $domain Unique identifier for retrieving translated strings
304 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
305 * where the .mo file resides. Deprecated, but still functional until 2.7
306 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR. This is the preferred argument to use. It takes precendence over $abs_rel_path
308 function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
309 $locale = get_locale();
311 if ( false !== $plugin_rel_path )
312 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
313 else if ( false !== $abs_rel_path)
314 $path = ABSPATH . trim( $abs_rel_path, '/');
316 $path = WP_PLUGIN_DIR;
318 $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
319 load_textdomain($domain, $mofile);
323 * Loads the theme's translated strings.
325 * If the current locale exists as a .mo file in the theme's root directory, it
326 * will be included in the translated strings by the $domain.
328 * The .mo files must be named based on the locale exactly.
332 * @param string $domain Unique identifier for retrieving translated strings
334 function load_theme_textdomain($domain, $path = false) {
335 $locale = get_locale();
337 $path = ( empty( $path ) ) ? get_template_directory() : $path;
339 $mofile = "$path/$locale.mo";
340 load_textdomain($domain, $mofile);