有没有像switch_to_blog()
这样的方法用于在 WordPress 中切换语言。
就像是
global $locale
$currentLanguage = $locale;
switch_to_language('de_DE');
//do some action with german localisation
switch_to_language($currentLanguage);
这通常可以用 WordPress 实现吗?
最佳答案
所以我终于找到了解决方案。该函数名为 load_textdomain()
这就是我这边的做法。请记住定义 LANGUAGE_PATH
以及您想切换到的语言 $new_language
. $your_domain
是你的插件/主题的文本域
//will output "Good Morning"
_e('Good Morning', $your_domain);
global $locale;
//save the current language for later
$current_language = $locale;
$new_language = 'DE_de';
//load the new text domain
load_textdomain( $your_language_domain, LANGUAGE_PATH.'/'.$your_domain.'-'.$new_language.'.mo' );
//do some action with the new localisation
//will output "Guten Morgen"
_e('Good Morning', $your_domain);
//go back to the previous language
load_textdomain( $your_language_domain , LANGUAGE_PATH.'/'.$your_domain.'-'.$current_language.'.mo' );
花了一段时间才在核心中找到这个方法。在 the codex site 上阅读有关该功能的更多信息
关于wordpress - 如何在 WordPress 中切换语言 "on-the-fly",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17229809/