Allow plugins to override the behaviour of load_textdomain() in a variety of flexible ways. Fixes #11012 props johanee and nbachiyski.

git-svn-id: http://svn.automattic.com/wordpress/trunk@12251 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2009-11-21 09:28:32 +00:00
parent d2fa378957
commit 5994455911
1 changed files with 28 additions and 18 deletions

View File

@ -28,17 +28,17 @@
function get_locale() { function get_locale() {
global $locale; global $locale;
if (isset($locale)) if ( isset( $locale ) )
return apply_filters( 'locale', $locale ); return apply_filters( 'locale', $locale );
// WPLANG is defined in wp-config. // WPLANG is defined in wp-config.
if (defined('WPLANG')) if ( defined( 'WPLANG' ) )
$locale = WPLANG; $locale = WPLANG;
if (empty($locale)) if ( empty( $locale ) )
$locale = 'en_US'; $locale = 'en_US';
return apply_filters('locale', $locale); return apply_filters( 'locale', $locale );
} }
/** /**
@ -56,7 +56,7 @@ function get_locale() {
*/ */
function translate( $text, $domain = 'default' ) { function translate( $text, $domain = 'default' ) {
$translations = &get_translations_for_domain( $domain ); $translations = &get_translations_for_domain( $domain );
return apply_filters('gettext', $translations->translate($text), $text, $domain); return apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
} }
function before_last_bar( $string ) { function before_last_bar( $string ) {
@ -80,12 +80,11 @@ function before_last_bar( $string ) {
*/ */
function translate_with_context( $text, $domain = 'default' ) { function translate_with_context( $text, $domain = 'default' ) {
return before_last_bar( translate( $text, $domain ) ); return before_last_bar( translate( $text, $domain ) );
} }
function translate_with_gettext_context( $text, $context, $domain = 'default' ) { function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
$translations = &get_translations_for_domain( $domain ); $translations = &get_translations_for_domain( $domain );
return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain); return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain );
} }
/** /**
@ -195,8 +194,8 @@ function esc_html_e( $text, $domain = 'default' ) {
* @param string $domain Optional. Domain to retrieve the translated text * @param string $domain Optional. Domain to retrieve the translated text
* @return string Translated context string without pipe * @return string Translated context string without pipe
*/ */
function _c($text, $domain = 'default') { function _c( $text, $domain = 'default' ) {
return translate_with_context($text, $domain); return translate_with_context( $text, $domain );
} }
function _x( $single, $context, $domain = 'default' ) { function _x( $single, $context, $domain = 'default' ) {
@ -239,7 +238,7 @@ function __ngettext() {
* @param string $domain Optional. The domain identifier the text should be retrieved in * @param string $domain Optional. The domain identifier the text should be retrieved in
* @return string Either $single or $plural translated text * @return string Either $single or $plural translated text
*/ */
function _n($single, $plural, $number, $domain = 'default') { function _n( $single, $plural, $number, $domain = 'default' ) {
$translations = &get_translations_for_domain( $domain ); $translations = &get_translations_for_domain( $domain );
$translation = $translations->translate_plural( $single, $plural, $number ); $translation = $translations->translate_plural( $single, $plural, $number );
return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain ); return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
@ -320,8 +319,18 @@ function _nx_noop( $single, $plural, $context ) {
* @param string $mofile Path to the .mo file * @param string $mofile Path to the .mo file
* @return bool true on success, false on failure * @return bool true on success, false on failure
*/ */
function load_textdomain($domain, $mofile) { function load_textdomain( $domain, $mofile ) {
global $l10n; global $l10n;
$plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
if ( true == $plugin_override ) {
return true;
}
do_action( 'load_textdomain', $domain, $mofile );
$mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
if ( !is_readable( $mofile ) ) return false; if ( !is_readable( $mofile ) ) return false;
@ -332,6 +341,7 @@ function load_textdomain($domain, $mofile) {
$mo->merge_with( $l10n[$domain] ); $mo->merge_with( $l10n[$domain] );
$l10n[$domain] = &$mo; $l10n[$domain] = &$mo;
return true; return true;
} }
@ -348,7 +358,7 @@ function load_default_textdomain() {
$mofile = WP_LANG_DIR . "/$locale.mo"; $mofile = WP_LANG_DIR . "/$locale.mo";
return load_textdomain('default', $mofile); return load_textdomain( 'default', $mofile );
} }
/** /**
@ -364,18 +374,18 @@ function load_default_textdomain() {
* where the .mo file resides. Deprecated, but still functional until 2.7 * where the .mo file resides. Deprecated, but still functional until 2.7
* @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 * @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
*/ */
function load_plugin_textdomain($domain, $abs_rel_path = false, $plugin_rel_path = false) { function load_plugin_textdomain( $domain, $abs_rel_path = false, $plugin_rel_path = false ) {
$locale = get_locale(); $locale = get_locale();
if ( false !== $plugin_rel_path ) if ( false !== $plugin_rel_path )
$path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/'); $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
else if ( false !== $abs_rel_path) else if ( false !== $abs_rel_path )
$path = ABSPATH . trim( $abs_rel_path, '/'); $path = ABSPATH . trim( $abs_rel_path, '/' );
else else
$path = WP_PLUGIN_DIR; $path = WP_PLUGIN_DIR;
$mofile = $path . '/'. $domain . '-' . $locale . '.mo'; $mofile = $path . '/'. $domain . '-' . $locale . '.mo';
return load_textdomain($domain, $mofile); return load_textdomain( $domain, $mofile );
} }
/** /**
@ -429,7 +439,7 @@ function load_child_theme_textdomain($domain, $path = false) {
*/ */
function &get_translations_for_domain( $domain ) { function &get_translations_for_domain( $domain ) {
global $l10n; global $l10n;
if ( !isset($l10n[$domain]) ) { if ( !isset( $l10n[$domain] ) ) {
$l10n[$domain] = &new NOOP_Translations; $l10n[$domain] = &new NOOP_Translations;
} }
return $l10n[$domain]; return $l10n[$domain];