web/wp-includes/l10n.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    26  * @return string The locale of the blog or from the 'locale' hook.
    26  * @return string The locale of the blog or from the 'locale' hook.
    27  */
    27  */
    28 function get_locale() {
    28 function get_locale() {
    29 	global $locale;
    29 	global $locale;
    30 
    30 
    31 	if (isset($locale))
    31 	if ( isset( $locale ) )
    32 		return apply_filters( 'locale', $locale );
    32 		return apply_filters( 'locale', $locale );
    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 (empty($locale))
    38 	if ( empty( $locale ) )
    39 		$locale = 'en_US';
    39 		$locale = 'en_US';
    40 
    40 
    41 	return apply_filters('locale', $locale);
    41 	return apply_filters( 'locale', $locale );
    42 }
    42 }
    43 
    43 
    44 /**
    44 /**
    45  * Retrieves the translation of $text. If there is no translation, or
    45  * Retrieves the translation of $text. If there is no translation, or
    46  * the domain isn't loaded the original text is returned.
    46  * the domain isn't loaded the original text is returned.
    54  * @param string $domain Domain to retrieve the translated text.
    54  * @param string $domain Domain to retrieve the translated text.
    55  * @return string Translated text
    55  * @return string Translated text
    56  */
    56  */
    57 function translate( $text, $domain = 'default' ) {
    57 function translate( $text, $domain = 'default' ) {
    58 	$translations = &get_translations_for_domain( $domain );
    58 	$translations = &get_translations_for_domain( $domain );
    59 	return apply_filters('gettext', $translations->translate($text), $text, $domain);
    59 	return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
    60 }
    60 }
    61 
    61 
    62 function before_last_bar( $string ) {
    62 function before_last_bar( $string ) {
    63 	$last_bar = strrpos( $string, '|' );
    63 	$last_bar = strrpos( $string, '|' );
    64 	if ( false == $last_bar )
    64 	if ( false == $last_bar )
    78  * @param string $domain Domain to retrieve the translated text
    78  * @param string $domain Domain to retrieve the translated text
    79  * @return string Translated text
    79  * @return string Translated text
    80  */
    80  */
    81 function translate_with_context( $text, $domain = 'default' ) {
    81 function translate_with_context( $text, $domain = 'default' ) {
    82 	return before_last_bar( translate( $text, $domain ) );
    82 	return before_last_bar( translate( $text, $domain ) );
    83 
       
    84 }
    83 }
    85 
    84 
    86 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
    85 function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
    87 	$translations = &get_translations_for_domain( $domain );
    86 	$translations = &get_translations_for_domain( $domain );
    88 	return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain);
    87 	return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
    89 }
    88 }
    90 
    89 
    91 /**
    90 /**
    92  * Retrieves the translation of $text. If there is no translation, or
    91  * Retrieves the translation of $text. If there is no translation, or
    93  * the domain isn't loaded the original text is returned.
    92  * the domain isn't loaded the original text is returned.
   175 function esc_html_e( $text, $domain = 'default' ) {
   174 function esc_html_e( $text, $domain = 'default' ) {
   176 	echo esc_html( translate( $text, $domain ) );
   175 	echo esc_html( translate( $text, $domain ) );
   177 }
   176 }
   178 
   177 
   179 /**
   178 /**
   180  * Retrieve translated string with vertical bar context
   179  * Retrieve translated string with gettext context
   181  *
   180  *
   182  * Quite a few times, there will be collisions with similar translatable text
   181  * Quite a few times, there will be collisions with similar translatable text
   183  * found in more than two places but with different translated context.
   182  * found in more than two places but with different translated context.
   184  *
   183  *
   185  * In order to use the separate contexts, the _c() function is used and the
   184  * By including the context in the pot file translators can translate the two
   186  * translatable string uses a pipe ('|') which has the context the string is in.
   185  * string differently
   187  *
   186  *
   188  * When the translated string is returned, it is everything before the pipe, not
   187  * @since 2.8
   189  * including the pipe character. If there is no pipe in the translated text then
   188  *
   190  * everything is returned.
   189  * @param string $text Text to translate
   191  *
   190  * @param string $context Context information for the translators
   192  * @since 2.2.0
       
   193  *
       
   194  * @param string $text Text to translate
       
   195  * @param string $domain Optional. Domain to retrieve the translated text
   191  * @param string $domain Optional. Domain to retrieve the translated text
   196  * @return string Translated context string without pipe
   192  * @return string Translated context string without pipe
   197  */
   193  */
   198 function _c($text, $domain = 'default') {
       
   199 	return translate_with_context($text, $domain);
       
   200 }
       
   201 
   194 
   202 function _x( $single, $context, $domain = 'default' ) {
   195 function _x( $single, $context, $domain = 'default' ) {
   203 	return translate_with_gettext_context( $single, $context, $domain );
   196 	return translate_with_gettext_context( $single, $context, $domain );
   204 }
   197 }
   205 
   198 
   206 function esc_attr_x( $single, $context, $domain = 'default' ) {
   199 function esc_attr_x( $single, $context, $domain = 'default' ) {
   207 	return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
   200 	return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
       
   201 }
       
   202 
       
   203 function esc_html_x( $single, $context, $domain = 'default' ) {
       
   204 	return esc_html( translate_with_gettext_context( $single, $context, $domain ) );
   208 }
   205 }
   209 
   206 
   210 function __ngettext() {
   207 function __ngettext() {
   211 	_deprecated_function( __FUNCTION__, '2.8', '_n()' );
   208 	_deprecated_function( __FUNCTION__, '2.8', '_n()' );
   212 	$args = func_get_args();
   209 	$args = func_get_args();
   233  * @param string $plural The text that will be used if $number is not 1
   230  * @param string $plural The text that will be used if $number is not 1
   234  * @param int $number The number to compare against to use either $single or $plural
   231  * @param int $number The number to compare against to use either $single or $plural
   235  * @param string $domain Optional. The domain identifier the text should be retrieved in
   232  * @param string $domain Optional. The domain identifier the text should be retrieved in
   236  * @return string Either $single or $plural translated text
   233  * @return string Either $single or $plural translated text
   237  */
   234  */
   238 function _n($single, $plural, $number, $domain = 'default') {
   235 function _n( $single, $plural, $number, $domain = 'default' ) {
   239 	$translations = &get_translations_for_domain( $domain );
   236 	$translations = &get_translations_for_domain( $domain );
   240 	$translation = $translations->translate_plural( $single, $plural, $number );
   237 	$translation = $translations->translate_plural( $single, $plural, $number );
   241 	return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
   238 	return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
   242 }
   239 }
   243 
   240 
   251 }
   248 }
   252 
   249 
   253 function _nx($single, $plural, $number, $context, $domain = 'default') {
   250 function _nx($single, $plural, $number, $context, $domain = 'default') {
   254 	$translations = &get_translations_for_domain( $domain );
   251 	$translations = &get_translations_for_domain( $domain );
   255 	$translation = $translations->translate_plural( $single, $plural, $number, $context );
   252 	$translation = $translations->translate_plural( $single, $plural, $number, $context );
   256 	return apply_filters( 'ngettext_with_context ', $translation, $single, $plural, $number, $context, $domain );
   253 	return apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
   257 }
   254 }
   258 
   255 
   259 /**
   256 /**
   260  * @deprecated Use _n_noop()
   257  * @deprecated Use _n_noop()
   261  */
   258  */
   314  *
   311  *
   315  * @param string $domain Unique identifier for retrieving translated strings
   312  * @param string $domain Unique identifier for retrieving translated strings
   316  * @param string $mofile Path to the .mo file
   313  * @param string $mofile Path to the .mo file
   317  * @return bool true on success, false on failure
   314  * @return bool true on success, false on failure
   318  */
   315  */
   319 function load_textdomain($domain, $mofile) {
   316 function load_textdomain( $domain, $mofile ) {
   320 	global $l10n;
   317 	global $l10n;
       
   318 	
       
   319 	$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
       
   320 	
       
   321 	if ( true == $plugin_override ) {
       
   322 		return true;
       
   323 	}
       
   324 	
       
   325 	do_action( 'load_textdomain', $domain, $mofile );
       
   326 		
       
   327 	$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
   321 
   328 
   322 	if ( !is_readable( $mofile ) ) return false;
   329 	if ( !is_readable( $mofile ) ) return false;
   323 
   330 
   324 	$mo = new MO();
   331 	$mo = new MO();
   325 	if ( !$mo->import_from_file( $mofile ) ) return false;
   332 	if ( !$mo->import_from_file( $mofile ) ) return false;
   326 
   333 
   327 	if ( isset( $l10n[$domain] ) )
   334 	if ( isset( $l10n[$domain] ) )
   328 		$mo->merge_with( $l10n[$domain] );
   335 		$mo->merge_with( $l10n[$domain] );
   329 
   336 
   330 	$l10n[$domain] = &$mo;
   337 	$l10n[$domain] = &$mo;
       
   338 	
   331 	return true;
   339 	return true;
   332 }
   340 }
   333 
   341 
   334 /**
   342 /**
   335  * Loads default translated strings based on locale.
   343  * Loads default translated strings based on locale.
   342 function load_default_textdomain() {
   350 function load_default_textdomain() {
   343 	$locale = get_locale();
   351 	$locale = get_locale();
   344 
   352 
   345 	$mofile = WP_LANG_DIR . "/$locale.mo";
   353 	$mofile = WP_LANG_DIR . "/$locale.mo";
   346 
   354 
   347 	return load_textdomain('default', $mofile);
   355 	return load_textdomain( 'default', $mofile );
   348 }
   356 }
   349 
   357 
   350 /**
   358 /**
   351  * Loads the plugin's translated strings.
   359  * Loads the plugin's translated strings.
   352  *
   360  *
   358  * @param string $domain Unique identifier for retrieving translated strings
   366  * @param string $domain Unique identifier for retrieving translated strings
   359  * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
   367  * @param string $abs_rel_path Optional. Relative path to ABSPATH of a folder,
   360  * 	where the .mo file resides. Deprecated, but still functional until 2.7
   368  * 	where the .mo file resides. Deprecated, but still functional until 2.7
   361  * @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
   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
   362  */
   370  */
   363 function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) {
   371 function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) {
   364 	$locale = get_locale();
   372 	$locale = get_locale();
   365 
   373 
   366 	if ( false !== $plugin_rel_path	)
   374 	if ( false !== $plugin_rel_path	)
   367 		$path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/');
   375 		$path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
   368 	else if ( false !== $abs_rel_path)
   376 	else if ( false !== $abs_rel_path )
   369 		$path = ABSPATH . trim( $abs_rel_path, '/');
   377 		$path = ABSPATH . trim( $abs_rel_path, '/' );
   370 	else
   378 	else
   371 		$path = WP_PLUGIN_DIR;
   379 		$path = WP_PLUGIN_DIR;
   372 
   380 
   373 	$mofile = $path . '/'. $domain . '-' . $locale . '.mo';
   381 	$mofile = $path . '/'. $domain . '-' . $locale . '.mo';
   374 	return load_textdomain($domain, $mofile);
   382 	return load_textdomain( $domain, $mofile );
   375 }
   383 }
   376 
   384 
   377 /**
   385 /**
   378  * Loads the theme's translated strings.
   386  * Loads the theme's translated strings.
   379  *
   387  *
   394 	$mofile = "$path/$locale.mo";
   402 	$mofile = "$path/$locale.mo";
   395 	return load_textdomain($domain, $mofile);
   403 	return load_textdomain($domain, $mofile);
   396 }
   404 }
   397 
   405 
   398 /**
   406 /**
       
   407  * Loads the child themes translated strings.
       
   408  *
       
   409  * If the current locale exists as a .mo file in the child themes root directory, it
       
   410  * will be included in the translated strings by the $domain.
       
   411  *
       
   412  * The .mo files must be named based on the locale exactly.
       
   413  *
       
   414  * @since 2.9.0
       
   415  *
       
   416  * @param string $domain Unique identifier for retrieving translated strings
       
   417  */
       
   418 function load_child_theme_textdomain($domain, $path = false) {
       
   419         $locale = get_locale();
       
   420 
       
   421         $path = ( empty( $path ) ) ? get_stylesheet_directory() : $path;
       
   422 
       
   423         $mofile = "$path/$locale.mo";
       
   424         return load_textdomain($domain, $mofile);
       
   425 }
       
   426 
       
   427 /**
   399  * Returns the Translations instance for a domain. If there isn't one,
   428  * Returns the Translations instance for a domain. If there isn't one,
   400  * returns empty Translations instance.
   429  * returns empty Translations instance.
   401  *
   430  *
   402  * @param string $domain
   431  * @param string $domain
   403  * @return object A Translation instance
   432  * @return object A Translation instance
   404  */
   433  */
   405 function &get_translations_for_domain( $domain ) {
   434 function &get_translations_for_domain( $domain ) {
   406 	global $l10n;
   435 	global $l10n;
   407 	$empty = &new Translations;
   436 	if ( !isset( $l10n[$domain] ) ) {
   408 	if ( isset($l10n[$domain]) )
   437 		$l10n[$domain] = &new NOOP_Translations;
   409 		return $l10n[$domain];
   438 	}
   410 	else
   439 	return $l10n[$domain];
   411 		return $empty;
       
   412 }
   440 }
   413 
   441 
   414 /**
   442 /**
   415  * Translates role name. Since the role names are in the database and
   443  * Translates role name. Since the role names are in the database and
   416  * not in the source there are dummy gettext calls to get them into the POT
   444  * not in the source there are dummy gettext calls to get them into the POT