web/wp-includes/l10n.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    14  *
    14  *
    15  * If the locale is not set already, then the WPLANG constant is used if it is
    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
    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.
    17  * for the locale global set and the locale is returned.
    18  *
    18  *
    19  * The process to get the locale should only be done once but the locale will
    19  * The process to get the locale should only be done once, but the locale will
    20  * always be filtered using the 'locale' hook.
    20  * always be filtered using the 'locale' hook.
    21  *
    21  *
    22  * @since 1.5.0
    22  * @since 1.5.0
    23  * @uses apply_filters() Calls 'locale' hook on locale value.
    23  * @uses apply_filters() Calls 'locale' hook on locale value.
    24  * @uses $locale Gets the locale stored in the global.
    24  * @uses $locale Gets the locale stored in the global.
    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
   102 	return translate( $text, $domain );
    97 	return translate( $text, $domain );
   103 }
    98 }
   104 
    99 
   105 /**
   100 /**
   106  * Retrieves the translation of $text and escapes it for safe use in an attribute.
   101  * Retrieves the translation of $text and escapes it for safe use in an attribute.
   107  * If there is no translation, or the domain isn't loaded the original text is returned.
   102  * If there is no translation, or the domain isn't loaded, the original text is returned.
   108  *
   103  *
   109  * @see translate() An alias of translate()
   104  * @see translate() An alias of translate()
   110  * @see esc_attr()
   105  * @see esc_attr()
   111  * @since 2.8.0
   106  * @since 2.8.0
   112  *
   107  *
   118 	return esc_attr( translate( $text, $domain ) );
   113 	return esc_attr( translate( $text, $domain ) );
   119 }
   114 }
   120 
   115 
   121 /**
   116 /**
   122  * Retrieves the translation of $text and escapes it for safe use in HTML output.
   117  * Retrieves the translation of $text and escapes it for safe use in HTML output.
   123  * If there is no translation, or the domain isn't loaded the original text is returned.
   118  * If there is no translation, or the domain isn't loaded, the original text is returned.
   124  *
   119  *
   125  * @see translate() An alias of translate()
   120  * @see translate() An alias of translate()
   126  * @see esc_html()
   121  * @see esc_html()
   127  * @since 2.8.0
   122  * @since 2.8.0
   128  *
   123  *
   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  *
   392  *
   455  *
   393  * @since 1.5.0
   456  * @since 1.5.0
   394  *
   457  *
   395  * @param string $domain Unique identifier for retrieving translated strings
   458  * @param string $domain Unique identifier for retrieving translated strings
   396  */
   459  */
   397 function load_theme_textdomain($domain, $path = false) {
   460 function load_theme_textdomain( $domain, $path = false ) {
   398 	$locale = get_locale();
   461 	$locale = apply_filters( 'theme_locale', get_locale(), $domain );
   399 
   462 
   400 	$path = ( empty( $path ) ) ? get_template_directory() : $path;
   463 	$path = ( empty( $path ) ) ? get_template_directory() : $path;
   401 
   464 
   402 	$mofile = "$path/$locale.mo";
   465 	$mofile = "$path/$locale.mo";
   403 	return load_textdomain($domain, $mofile);
   466 	return load_textdomain($domain, $mofile);
   413  *
   476  *
   414  * @since 2.9.0
   477  * @since 2.9.0
   415  *
   478  *
   416  * @param string $domain Unique identifier for retrieving translated strings
   479  * @param string $domain Unique identifier for retrieving translated strings
   417  */
   480  */
   418 function load_child_theme_textdomain($domain, $path = false) {
   481 function load_child_theme_textdomain( $domain, $path = false ) {
   419         $locale = get_locale();
   482 	$locale = apply_filters( 'theme_locale', get_locale(), $domain );
   420 
   483 
   421         $path = ( empty( $path ) ) ? get_stylesheet_directory() : $path;
   484 	$path = ( empty( $path ) ) ? get_stylesheet_directory() : $path;
   422 
   485 
   423         $mofile = "$path/$locale.mo";
   486 	$mofile = "$path/$locale.mo";
   424         return load_textdomain($domain, $mofile);
   487 	return load_textdomain($domain, $mofile);
   425 }
   488 }
   426 
   489 
   427 /**
   490 /**
   428  * Returns the Translations instance for a domain. If there isn't one,
   491  * Returns the Translations instance for a domain. If there isn't one,
   429  * returns empty Translations instance.
   492  * returns empty Translations instance.
   432  * @return object A Translation instance
   495  * @return object A Translation instance
   433  */
   496  */
   434 function &get_translations_for_domain( $domain ) {
   497 function &get_translations_for_domain( $domain ) {
   435 	global $l10n;
   498 	global $l10n;
   436 	if ( !isset( $l10n[$domain] ) ) {
   499 	if ( !isset( $l10n[$domain] ) ) {
   437 		$l10n[$domain] = &new NOOP_Translations;
   500 		$l10n[$domain] = new NOOP_Translations;
   438 	}
   501 	}
   439 	return $l10n[$domain];
   502 	return $l10n[$domain];
       
   503 }
       
   504 
       
   505 /**
       
   506  * Whether there are translations for the domain
       
   507  *
       
   508  * @since 3.0.0
       
   509  * @param string $domain
       
   510  * @return bool Whether there are translations
       
   511  */
       
   512 function is_textdomain_loaded( $domain ) {
       
   513 	global $l10n;
       
   514 	return isset( $l10n[$domain] );
   440 }
   515 }
   441 
   516 
   442 /**
   517 /**
   443  * Translates role name. Since the role names are in the database and
   518  * Translates role name. Since the role names are in the database and
   444  * not in the source there are dummy gettext calls to get them into the POT
   519  * not in the source there are dummy gettext calls to get them into the POT
   450  * won't suffer from that problem.
   525  * won't suffer from that problem.
   451  */
   526  */
   452 function translate_user_role( $name ) {
   527 function translate_user_role( $name ) {
   453 	return translate_with_gettext_context( before_last_bar($name), 'User role' );
   528 	return translate_with_gettext_context( before_last_bar($name), 'User role' );
   454 }
   529 }
   455 ?>
   530 
       
   531 /**
       
   532  * Get all available languages based on the presence of *.mo files in a given directory. The default directory is WP_LANG_DIR.
       
   533  *
       
   534  * @since 3.0.0
       
   535  *
       
   536  * @param string $dir A directory in which to search for language files. The default directory is WP_LANG_DIR.
       
   537  * @return array Array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names.
       
   538  */
       
   539 function get_available_languages( $dir = null ) {
       
   540 	$languages = array();
       
   541 
       
   542 	foreach( (array)glob( ( is_null( $dir) ? WP_LANG_DIR : $dir ) . '/*.mo' ) as $lang_file ) {
       
   543 		$lang_file = basename($lang_file, '.mo');
       
   544 		if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
       
   545 			0 !== strpos( $lang_file, 'admin-' ))
       
   546 			$languages[] = $lang_file;
       
   547 	}
       
   548 
       
   549 	return $languages;
       
   550 }