【问题标题】:Applying translation to custom WordPress plugin将翻译应用于自定义 WordPress 插件
【发布时间】:2017-09-06 13:50:22
【问题描述】:

我正在尝试将翻译应用到我的自定义插件。我已经创建了 brau-ru_RU.mobrau-ru_RU.po 文件。我的翻译域是“brau”。

我尝试将文件放入 wp-content/languages/plugins/ 并在我的插件中执行此代码。

$domain = 'brau';
$mo_file = WP_LANG_DIR.'/plugins/'.$domain.'-'.get_locale(). '.mo';

var_dump(load_textdomain( $domain, $mo_file )); 
var_dump(load_plugin_textdomain( $domain ));
var_dump(__('This is the test', 'brau'));

结果是:

bool(true) bool(true) string(16) "This is the test"

我的配置中也有这段代码

define ('WPLANG', 'ru_RU');

文本应该从英语翻译成俄语,但事实并非如此。我错过了什么?

这是插件测试版的链接:https://github.com/Brezgalov/brezgalovauth

【问题讨论】:

  • 您的 get_locale() 是否返回 ru_RU?
  • 路径是否正确,如果你 var_dump($mo_file);
  • 是的,get_locale 和 $mo_file 都可以,检查了两次

标签: wordpress


【解决方案1】:

如果您想将语言文件添加到插件中,则可以在插件文件夹中创建一个名为 languages 的文件夹,在插件主文件中您可以这样定义

// Localiztion with language files
function custom_langs_i18n() {
    load_plugin_textdomain( 'textdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
add_action( 'init', 'custom_langs_i18n' );

请记住在使用字符串的地方使用 textdomain。

【讨论】:

  • 我试过这样做,也没有成功。 load_plugin_textdomain 返回 true,文本仍然是 Eng。再试一次,没有任何变化
  • 您能分享您的插件以便我们检查问题吗?
  • 你的mo文件编译正确了吗?
  • 我添加了一个指向问题帖子的回购链接。我用poedit编译mo,mo文件会不会出错?
猜你喜欢
  • 2019-08-02
  • 2015-02-11
  • 2021-09-18
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多