wordpress - 如何在 WordPress 中切换语言 "on-the-fly"

标签 wordpress switch-statement translation

有没有像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/

相关文章:

email - Magento 翻译电子邮件 - 电子邮件标题

wordpress - WooCommerce - 按产品属性和类别过滤相关产品

php - Dropbox 帐户作为 Wordpress 上传文件夹

wordpress - 使用 ffmpeg 从视频中获取屏幕截图

Java - 方法程序 - 在方法中使用 switch case

c - switch语句循环

symfony - 使用 transchoice 和 lingohub 进行性别相关翻译

php - 将 JS 链接到 wordpress

c++ - 调用函数时切换 "transfer of control bypasses initialization of:"

symfony - Twig Symfony 5 中的复数