33 |
33 |
34 // WPLANG is defined in wp-config. |
34 // WPLANG is defined in wp-config. |
35 if ( defined( 'WPLANG' ) ) |
35 if ( defined( 'WPLANG' ) ) |
36 $locale = WPLANG; |
36 $locale = WPLANG; |
37 |
37 |
|
38 // If multisite, check options. |
|
39 if ( is_multisite() ) { |
|
40 // Don't check blog option when installing. |
|
41 if ( defined( 'WP_INSTALLING' ) || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) |
|
42 $ms_locale = get_site_option('WPLANG'); |
|
43 |
|
44 if ( $ms_locale !== false ) |
|
45 $locale = $ms_locale; |
|
46 } |
|
47 |
38 if ( empty( $locale ) ) |
48 if ( empty( $locale ) ) |
39 $locale = 'en_US'; |
49 $locale = 'en_US'; |
40 |
50 |
41 return apply_filters( 'locale', $locale ); |
51 return apply_filters( 'locale', $locale ); |
42 } |
52 } |
43 |
53 |
44 /** |
54 /** |
45 * Retrieves the translation of $text. If there is no translation, or |
55 * Retrieves the translation of $text. If there is no translation, or |
46 * the domain isn't loaded the original text is returned. |
56 * the domain isn't loaded, the original text is returned. |
47 * |
57 * |
48 * @see __() Don't use translate() directly, use __() |
58 * @see __() Don't use translate() directly, use __() |
49 * @since 2.2.0 |
59 * @since 2.2.0 |
50 * @uses apply_filters() Calls 'gettext' on domain translated text |
60 * @uses apply_filters() Calls 'gettext' on domain translated text |
51 * with the untranslated text as second parameter. |
61 * with the untranslated text as second parameter. |
65 return $string; |
75 return $string; |
66 else |
76 else |
67 return substr( $string, 0, $last_bar ); |
77 return substr( $string, 0, $last_bar ); |
68 } |
78 } |
69 |
79 |
70 /** |
|
71 * Translates $text like translate(), but assumes that the text |
|
72 * contains a context after its last vertical bar. |
|
73 * |
|
74 * @since 2.5 |
|
75 * @uses translate() |
|
76 * |
|
77 * @param string $text Text to translate |
|
78 * @param string $domain Domain to retrieve the translated text |
|
79 * @return string Translated text |
|
80 */ |
|
81 function translate_with_context( $text, $domain = 'default' ) { |
|
82 return before_last_bar( translate( $text, $domain ) ); |
|
83 } |
|
84 |
|
85 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { |
80 function translate_with_gettext_context( $text, $context, $domain = 'default' ) { |
86 $translations = &get_translations_for_domain( $domain ); |
81 $translations = &get_translations_for_domain( $domain ); |
87 return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain ); |
82 return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain ); |
88 } |
83 } |
89 |
84 |
90 /** |
85 /** |
91 * Retrieves the translation of $text. If there is no translation, or |
86 * Retrieves the translation of $text. If there is no translation, or |
92 * the domain isn't loaded the original text is returned. |
87 * the domain isn't loaded, the original text is returned. |
93 * |
88 * |
94 * @see translate() An alias of translate() |
89 * @see translate() An alias of translate() |
95 * @since 2.1.0 |
90 * @since 2.1.0 |
96 * |
91 * |
97 * @param string $text Text to translate |
92 * @param string $text Text to translate |
180 * |
175 * |
181 * Quite a few times, there will be collisions with similar translatable text |
176 * Quite a few times, there will be collisions with similar translatable text |
182 * found in more than two places but with different translated context. |
177 * found in more than two places but with different translated context. |
183 * |
178 * |
184 * By including the context in the pot file translators can translate the two |
179 * By including the context in the pot file translators can translate the two |
185 * string differently |
180 * strings differently. |
186 * |
181 * |
187 * @since 2.8 |
182 * @since 2.8.0 |
188 * |
183 * |
189 * @param string $text Text to translate |
184 * @param string $text Text to translate |
190 * @param string $context Context information for the translators |
185 * @param string $context Context information for the translators |
191 * @param string $domain Optional. Domain to retrieve the translated text |
186 * @param string $domain Optional. Domain to retrieve the translated text |
192 * @return string Translated context string without pipe |
187 * @return string Translated context string without pipe |
193 */ |
188 */ |
194 |
189 function _x( $text, $context, $domain = 'default' ) { |
195 function _x( $single, $context, $domain = 'default' ) { |
190 return translate_with_gettext_context( $text, $context, $domain ); |
196 return translate_with_gettext_context( $single, $context, $domain ); |
191 } |
|
192 |
|
193 /** |
|
194 * Displays translated string with gettext context |
|
195 * |
|
196 * @see _x |
|
197 * @since 3.0.0 |
|
198 * |
|
199 * @param string $text Text to translate |
|
200 * @param string $context Context information for the translators |
|
201 * @param string $domain Optional. Domain to retrieve the translated text |
|
202 * @return string Translated context string without pipe |
|
203 */ |
|
204 function _ex( $text, $context, $domain = 'default' ) { |
|
205 echo _x( $text, $context, $domain ); |
197 } |
206 } |
198 |
207 |
199 function esc_attr_x( $single, $context, $domain = 'default' ) { |
208 function esc_attr_x( $single, $context, $domain = 'default' ) { |
200 return esc_attr( translate_with_gettext_context( $single, $context, $domain ) ); |
209 return esc_attr( translate_with_gettext_context( $single, $context, $domain ) ); |
201 } |
210 } |
202 |
211 |
203 function esc_html_x( $single, $context, $domain = 'default' ) { |
212 function esc_html_x( $single, $context, $domain = 'default' ) { |
204 return esc_html( translate_with_gettext_context( $single, $context, $domain ) ); |
213 return esc_html( translate_with_gettext_context( $single, $context, $domain ) ); |
205 } |
|
206 |
|
207 function __ngettext() { |
|
208 _deprecated_function( __FUNCTION__, '2.8', '_n()' ); |
|
209 $args = func_get_args(); |
|
210 return call_user_func_array('_n', $args); |
|
211 } |
214 } |
212 |
215 |
213 /** |
216 /** |
214 * Retrieve the plural or single form based on the amount. |
217 * Retrieve the plural or single form based on the amount. |
215 * |
218 * |
219 * If the domain does exist, then the parameters $single, $plural, and $number |
222 * If the domain does exist, then the parameters $single, $plural, and $number |
220 * will first be passed to the domain's ngettext method. Then it will be passed |
223 * will first be passed to the domain's ngettext method. Then it will be passed |
221 * to the 'ngettext' filter hook along with the same parameters. The expected |
224 * to the 'ngettext' filter hook along with the same parameters. The expected |
222 * type will be a string. |
225 * type will be a string. |
223 * |
226 * |
224 * @since 1.2.0 |
227 * @since 2.8.0 |
225 * @uses $l10n Gets list of domain translated string (gettext_reader) objects |
228 * @uses $l10n Gets list of domain translated string (gettext_reader) objects |
226 * @uses apply_filters() Calls 'ngettext' hook on domains text returned, |
229 * @uses apply_filters() Calls 'ngettext' hook on domains text returned, |
227 * along with $single, $plural, and $number parameters. Expected to return string. |
230 * along with $single, $plural, and $number parameters. Expected to return string. |
228 * |
231 * |
229 * @param string $single The text that will be used if $number is 1 |
232 * @param string $single The text that will be used if $number is 1 |
237 $translation = $translations->translate_plural( $single, $plural, $number ); |
240 $translation = $translations->translate_plural( $single, $plural, $number ); |
238 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); |
241 return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); |
239 } |
242 } |
240 |
243 |
241 /** |
244 /** |
242 * @see _n() A version of _n(), which supports contexts -- |
245 * A hybrid of _n() and _x(). It supports contexts and plurals. |
243 * strips everything from the translation after the last bar |
246 * |
244 * |
247 * @see _n() |
245 */ |
248 * @see _x() |
246 function _nc( $single, $plural, $number, $domain = 'default' ) { |
249 * |
247 return before_last_bar( _n( $single, $plural, $number, $domain ) ); |
250 */ |
248 } |
|
249 |
|
250 function _nx($single, $plural, $number, $context, $domain = 'default') { |
251 function _nx($single, $plural, $number, $context, $domain = 'default') { |
251 $translations = &get_translations_for_domain( $domain ); |
252 $translations = &get_translations_for_domain( $domain ); |
252 $translation = $translations->translate_plural( $single, $plural, $number, $context ); |
253 $translation = $translations->translate_plural( $single, $plural, $number, $context ); |
253 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); |
254 return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain ); |
254 } |
255 } |
255 |
256 |
256 /** |
257 /** |
257 * @deprecated Use _n_noop() |
|
258 */ |
|
259 function __ngettext_noop() { |
|
260 _deprecated_function( __FUNCTION__, '2.8', '_n_noop()' ); |
|
261 $args = func_get_args(); |
|
262 return call_user_func_array('_n_noop', $args); |
|
263 |
|
264 } |
|
265 |
|
266 /** |
|
267 * Register plural strings in POT file, but don't translate them. |
258 * Register plural strings in POT file, but don't translate them. |
268 * |
259 * |
269 * Used when you want do keep structures with translatable plural strings and |
260 * Used when you want to keep structures with translatable plural strings and |
270 * use them later. |
261 * use them later. |
271 * |
262 * |
272 * Example: |
263 * Example: |
273 * $messages = array( |
264 * $messages = array( |
274 * 'post' => _n_noop('%s post', '%s posts'), |
265 * 'post' => _n_noop('%s post', '%s posts'), |
275 * 'page' => _n_noop('%s pages', '%s pages') |
266 * 'page' => _n_noop('%s pages', '%s pages') |
276 * ); |
267 * ); |
277 * ... |
268 * ... |
278 * $message = $messages[$type]; |
269 * $message = $messages[$type]; |
279 * $usable_text = sprintf(_n($message[0], $message[1], $count), $count); |
270 * $usable_text = sprintf( translate_nooped_plural( $message, $count ), $count ); |
280 * |
271 * |
281 * @since 2.5 |
272 * @since 2.5 |
282 * @param $single Single form to be i18ned |
273 * @param string $singular Single form to be i18ned |
283 * @param $plural Plural form to be i18ned |
274 * @param string $plural Plural form to be i18ned |
284 * @return array array($single, $plural) |
275 * @param string $domain Optional. The domain identifier the text will be retrieved in |
285 */ |
276 * @return array array($singular, $plural) |
286 function _n_noop( $single, $plural ) { |
277 */ |
287 return array( $single, $plural ); |
278 function _n_noop( $singular, $plural, $domain = null ) { |
|
279 return array( 0 => $singular, 1 => $plural, 'singular' => $singular, 'plural' => $plural, 'context' => null, 'domain' => $domain ); |
288 } |
280 } |
289 |
281 |
290 /** |
282 /** |
291 * Register plural strings with context in POT file, but don't translate them. |
283 * Register plural strings with context in POT file, but don't translate them. |
292 * |
284 * |
293 * @see _n_noop() |
285 * @see _n_noop() |
294 */ |
286 */ |
295 function _nx_noop( $single, $plural, $context ) { |
287 function _nx_noop( $singular, $plural, $context, $domain = null ) { |
296 return array( $single, $plural, $context ); |
288 return array( 0 => $singular, 1 => $plural, 2 => $context, 'singular' => $singular, 'plural' => $plural, 'context' => $context, 'domain' => $domain ); |
297 } |
289 } |
298 |
290 |
|
291 /** |
|
292 * Translate the result of _n_noop() or _nx_noop() |
|
293 * |
|
294 * @since 3.1 |
|
295 * @param array $nooped_plural Array with singular, plural and context keys, usually the result of _n_noop() or _nx_noop() |
|
296 * @param int $count Number of objects |
|
297 * @param string $domain Optional. The domain identifier the text should be retrieved in. If $nooped_plural contains |
|
298 * a domain passed to _n_noop() or _nx_noop(), it will override this value. |
|
299 */ |
|
300 function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) { |
|
301 if ( $nooped_plural['domain'] ) |
|
302 $domain = $nooped_plural['domain']; |
|
303 |
|
304 if ( $nooped_plural['context'] ) |
|
305 return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain ); |
|
306 else |
|
307 return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain ); |
|
308 } |
299 |
309 |
300 /** |
310 /** |
301 * Loads a MO file into the domain $domain. |
311 * Loads a MO file into the domain $domain. |
302 * |
312 * |
303 * If the domain already exists, the translations will be merged. If both |
313 * If the domain already exists, the translations will be merged. If both |
309 * @since 1.5.0 |
319 * @since 1.5.0 |
310 * @uses $l10n Gets list of domain translated string objects |
320 * @uses $l10n Gets list of domain translated string objects |
311 * |
321 * |
312 * @param string $domain Unique identifier for retrieving translated strings |
322 * @param string $domain Unique identifier for retrieving translated strings |
313 * @param string $mofile Path to the .mo file |
323 * @param string $mofile Path to the .mo file |
314 * @return bool true on success, false on failure |
324 * @return bool True on success, false on failure |
315 */ |
325 */ |
316 function load_textdomain( $domain, $mofile ) { |
326 function load_textdomain( $domain, $mofile ) { |
317 global $l10n; |
327 global $l10n; |
318 |
328 |
319 $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile ); |
329 $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile ); |
320 |
330 |
321 if ( true == $plugin_override ) { |
331 if ( true == $plugin_override ) { |
322 return true; |
332 return true; |
323 } |
333 } |
324 |
334 |
|
335 //print( $domain." ". $mofile."<br>"); |
325 do_action( 'load_textdomain', $domain, $mofile ); |
336 do_action( 'load_textdomain', $domain, $mofile ); |
326 |
337 |
327 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); |
338 $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain ); |
328 |
339 |
329 if ( !is_readable( $mofile ) ) return false; |
340 if ( !is_readable( $mofile ) ) return false; |
330 |
341 |
331 $mo = new MO(); |
342 $mo = new MO(); |
333 |
344 |
334 if ( isset( $l10n[$domain] ) ) |
345 if ( isset( $l10n[$domain] ) ) |
335 $mo->merge_with( $l10n[$domain] ); |
346 $mo->merge_with( $l10n[$domain] ); |
336 |
347 |
337 $l10n[$domain] = &$mo; |
348 $l10n[$domain] = &$mo; |
338 |
349 |
339 return true; |
350 return true; |
340 } |
351 } |
341 |
352 |
342 /** |
353 /** |
|
354 * Unloads translations for a domain |
|
355 * |
|
356 * @since 3.0.0 |
|
357 * @param string $domain Textdomain to be unloaded |
|
358 * @return bool Whether textdomain was unloaded |
|
359 */ |
|
360 function unload_textdomain( $domain ) { |
|
361 global $l10n; |
|
362 |
|
363 $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain ); |
|
364 |
|
365 if ( $plugin_override ) |
|
366 return true; |
|
367 |
|
368 do_action( 'unload_textdomain', $domain ); |
|
369 |
|
370 if ( isset( $l10n[$domain] ) ) { |
|
371 unset( $l10n[$domain] ); |
|
372 return true; |
|
373 } |
|
374 |
|
375 return false; |
|
376 } |
|
377 |
|
378 /** |
343 * Loads default translated strings based on locale. |
379 * Loads default translated strings based on locale. |
344 * |
380 * |
345 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The |
381 * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The |
346 * translated (.mo) file is named based off of the locale. |
382 * translated (.mo) file is named based on the locale. |
347 * |
383 * |
348 * @since 1.5.0 |
384 * @since 1.5.0 |
349 */ |
385 */ |
350 function load_default_textdomain() { |
386 function load_default_textdomain() { |
351 $locale = get_locale(); |
387 $locale = get_locale(); |
352 |
388 |
353 $mofile = WP_LANG_DIR . "/$locale.mo"; |
389 load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" ); |
354 |
390 |
355 return load_textdomain( 'default', $mofile ); |
391 if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) { |
|
392 load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" ); |
|
393 return; |
|
394 } |
|
395 |
|
396 if ( is_admin() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) |
|
397 load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" ); |
|
398 |
|
399 if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) |
|
400 load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" ); |
|
401 |
356 } |
402 } |
357 |
403 |
358 /** |
404 /** |
359 * Loads the plugin's translated strings. |
405 * Loads the plugin's translated strings. |
360 * |
406 * |
364 * @since 1.5.0 |
410 * @since 1.5.0 |
365 * |
411 * |
366 * @param string $domain Unique identifier for retrieving translated strings |
412 * @param string $domain Unique identifier for retrieving translated strings |
367 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder, |
413 * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder, |
368 * where the .mo file resides. Deprecated, but still functional until 2.7 |
414 * where the .mo file resides. Deprecated, but still functional until 2.7 |
369 * @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 |
415 * @param string $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR. This is the preferred argument to use. It takes precedence over $abs_rel_path |
370 */ |
416 */ |
371 function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) { |
417 function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) { |
372 $locale = get_locale(); |
418 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); |
373 |
419 |
374 if ( false !== $plugin_rel_path ) |
420 if ( false !== $plugin_rel_path ) { |
375 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); |
421 $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); |
376 else if ( false !== $abs_rel_path ) |
422 } else if ( false !== $abs_rel_path ) { |
|
423 _deprecated_argument( __FUNCTION__, '2.7' ); |
377 $path = ABSPATH . trim( $abs_rel_path, '/' ); |
424 $path = ABSPATH . trim( $abs_rel_path, '/' ); |
378 else |
425 } else { |
379 $path = WP_PLUGIN_DIR; |
426 $path = WP_PLUGIN_DIR; |
|
427 } |
380 |
428 |
381 $mofile = $path . '/'. $domain . '-' . $locale . '.mo'; |
429 $mofile = $path . '/'. $domain . '-' . $locale . '.mo'; |
382 return load_textdomain( $domain, $mofile ); |
430 return load_textdomain( $domain, $mofile ); |
383 } |
431 } |
384 |
432 |
385 /** |
433 /** |
|
434 * Load the translated strings for a plugin residing in the mu-plugins dir. |
|
435 * |
|
436 * @since 3.0.0 |
|
437 * |
|
438 * @param string $domain Unique identifier for retrieving translated strings |
|
439 * @param string $mu_plugin_rel_path Relative to WPMU_PLUGIN_DIR directory in which |
|
440 * the MO file resides. Defaults to empty string. |
|
441 */ |
|
442 function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { |
|
443 $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); |
|
444 $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); |
|
445 load_textdomain( $domain, trailingslashit( $path ) . "$domain-$locale.mo" ); |
|
446 } |
|
447 |
|
448 /** |
386 * Loads the theme's translated strings. |
449 * Loads the theme's translated strings. |
387 * |
450 * |
388 * If the current locale exists as a .mo file in the theme's root directory, it |
451 * If the current locale exists as a .mo file in the theme's root directory, it |
389 * will be included in the translated strings by the $domain. |
452 * will be included in the translated strings by the $domain. |
390 * |
453 * |