问题描述
我知道我可以让我的翻译成 JavaScript:
$MyTranslations = array(
'translation1' => __("Some String 1", "MyTranslations"),
'translation2' => __("Some String 2", "MyTranslations")
);
wp_localize_script( 'jquery', 'my_translations', $MyTranslations );
但是看一些 WordPress TinyMCE 插件,他们使用这样的电话:
{
title : ed.getLang('advlink.link_desc'),
...
}
WordPress 如何将其翻译成 getLang()
?我应该这样做,或者我只是使用我的第一个代码,并直接访问变量:
{
title : my_translations.translation1,
...
}
最佳解决方案
使用过滤器'mce_external_languages'
。来自 wp-includes/class-wp-editor.php
:
The following filter loads external language files for TinyMCE plugins. It takes an associative array ‘plugin_name’ => ‘path’, where path is the include path to the file. The language file should follow the same format as
/tinymce/langs/wp-langs.php
and should define a variable $strings that holds all translated strings. When this filter is not used, the function will try to load{mce_locale}.js
. If that is not found, en.js will be tried next.
$mce_external_languages = apply_filters('mce_external_languages', array());
我只是使用 wp-includes/js/tinymce/langs/wp-langs.php
… 的副本,并放弃多余的 mce_escape()
赞成原来的 esc_js()
。
示例文件:
<?php # -*- coding: utf-8 -*-
$strings = 'tinyMCE.addI18n(
{' . _WP_Editors::$mce_locale . '.extrastrings:
{
helloworld: "' . esc_js( __( 'Hello World', 'my_plugin_text_domain' ) ) . '",
foobar: "' . esc_js( __( 'Foo Bar', 'my_plugin_text_domain' ) ) . '"
}
}
)';
在您的插件中,您只需使用:
add_filter( 'mce_external_languages', 'wpse_44785_add_tinymce_lang', 10, 1 );
function wpse_44785_add_tinymce_lang( $arr )
{
$arr[] = 'full_path_to_lang_file.php';
return $arr;
}
要使用 JavaScript 来访问新的字符串,例如:
title : ed.getLang('extrastrings.helloworld')
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。