好吧,尽管我想感谢莎莉·CJ的努力;不幸的是,他/她的解决方案对我的输出没有任何影响。在稍微挖掘并通读之后this 发帖后,我现在可以为您提供这个解决方案,它很有魅力(您可以将此代码作为sperate文件集成到您的plugins includes/php文件夹中,至少对我有用。我已经添加了名称空间等。所以只需使用尚未使用的名称空间名称,您就可以开始了):
<?php
// This script is used for special actions related to translations / locales
namespace YourNamespace;
/*########################################################
## TABLE OF CONTENTS ##
##------------------------------------------------------##
## 1. Switch locale used for muplugin translations ##
##------------------------------------------------------##
## 2. Reset locale used for muplugin translations ##
########################################################*/
if ( ! class_exists( \'YourNamespace\\LocalizationFeatures\' ) ) {
class LocalizationFeatures {
/*########################################################
## 1. Switch locale used for muplugin translations ##
#########################################################*/
/**
* If you\'re on a plugin\'s page with a loaded textdomain (let\'s say for
* ex. english), and you want to retrieve translations from that plugins\'
* .mo files accessed via __(), _e(), etc. in another language (e.g. es)
* you cannot achieve this via switch_locale() if your translations were
* actually defined via a given text domain. What has to be done in this
* case is to reload the textdomain in the targeted language, to update
* global $l10n variable, which is used as resource for the gettext
* translations wp retrieves.
*
* @param string $domain text domain to be reloaded in the specified
* target language
*
* @param string $locale two-digit locale which determines to which
* locale the function shall temp. switch;
* one of:
*
* "de" (which switches to root locale)
* "en" (which switches to locale en_GB)
* "fr" (which switches to locale fr_FR)
* "it" (which switches to locale it_IT)
* "es" (which switches to locale es_ES)
* "pt" (which switches to locale pt_PT)
*
* @param mixed value of global $l10n[{domain}] before the switch; false
* if it was not set by then (which is the case if the page
* was previously loaded in the website\'s default language)
* ALWAYS REMEMBER TO RESET $l10n[{domain}] after executing
* whatever needed to be executed after the switch!!! If it
* was not set (i.e. if this value is false); make sure to
* unset it again instead of reassigning it.
* To facilitate this whole procedure, a second callback is
* created; always to be called after the use of this one.
*/
public static function switch_textdomain( $domain, $locale, $answer ) {
// First of all, before switching, get the previous state of $l10n:
global $l10n;
$previous_language = isset( $l10n[$domain] ) ? $l10n[$domain] : false;
// Next, if the target locale is the default language, there will be no
// associated .mo - file to be loaded. To switch the textdomain being used
// in this case, we have to remove the $l10n[$domain] from the $l10n array
if ( $locale === "de" ) {
unset( $l10n[$domain] );
} else {
// If the target locale is different from the default language; load the
// according text domain
$target_locale = "";
switch ( $locale ) {
case \'en\':
$target_locale = "en_GB";
break;
case \'fr\':
$target_locale = "fr_FR";
break;
case \'es\':
$target_locale = "es_ES";
break;
case \'pt\':
$target_locale = "pt_PT";
break;
case \'it\':
$target_locale = "it_IT";
break;
}
if ( ! load_textdomain(
$domain,
WPMU_PLUGIN_DIR . "/{$domain}/languages/{$domain}-{$target_locale}.mo"
) ) {
// output error feedback you may want
}
}
return $previous_language;
} // end of switch_textdomain() function definition
/*##########################################################################
## 2. Reset locale used for muplugin translations (after calling 1.) ##
##########################################################################*/
/**
* This function is ALWAYS to be used after using self::switch_textdomain(),
* to always reset the textdomain\'s language back to its previous state
* before switching it; as soon as the wanted tasks have been executed.
*
* @param string $domain concerned text domain
* @param mixed $previous_l10n state of global $l10n[$domain] b4 switch
* corresponds to return value of
* self::switch_textdomain()
*/
public static function reset_textdomain( $domain, $previous_l10n ) {
global $l10n;
// If $previous_l10n is === false, this means that , before using
// self::switch_textdomain(), the default language was being used, so to
// reset it, we need to re-unset it, as it was set via
// self::switch_textdomain()
if ( $previous_l10n === false ) {
unset( $l10n[$domain] );
} else {
// If it was previously set to something, reset it back to that
$l10n[$domain] = $previous_l10n;
}
} // end of reset_textdomain() function definition
} // end of LocalizationFeatures class definition
} // loop which opens if there\'s currently no YourNamespace\\LocalizationFeatures class defined
?>
请注意,我的代码适用于MU插件;但是您应该能够通过使用
WP_PLUGIN_DIR
常量而不是
WPMU_PLUGIN_DIR
.
我详细阐述了我的研究总结出的想法;如果有任何错误,请随时更正。我也可以只做一个函数1。在执行任何操作后(例如,使用传递给1的附加参数),将重置回以前的textdomain区域设置。这是一个回调*args
或者别的什么。这个设计更适合我这个项目的插件。
UPDATE
这个解决方案似乎并不完美;一旦您使用本地化页面更改管理用户的语言;当涉及到从服务器动态检索本地化时,情况变得非常糟糕。实际上,我可以在一个不使用脚本的环境中重现这个问题;这就是为什么我认为这可能是一个无关的问题,但也可能是相关的;我很困惑tbh。更多详情;请在此检查:Changing wp admin user's language does not update all of the plugin localizations in backend.