mce_external_languages
云策文档标注
概述
mce_external_languages 是一个 WordPress 过滤器,用于加载外部 TinyMCE 3.x 插件的翻译文件。它允许开发者通过关联数组指定插件名称和翻译文件路径,以支持多语言界面。
关键要点
- 过滤器参数:$translations 是一个关联数组,键为插件名,值为翻译文件路径;$editor_id 是编辑器唯一标识符,如 'content'。
- 翻译文件格式:需遵循 wp_mce_translation() 的格式,定义 $strings 变量存储翻译字符串,并返回 JavaScript 代码以通过 tinyMCE.addI18n 添加翻译。
- 使用场景:适用于为自定义 TinyMCE 插件添加本地化支持,确保翻译文件正确加载并在 JavaScript 中通过 ed.getLang() 调用。
代码示例
add_filter('mce_external_languages', 'my_custom_tinymce_plugin_add_locale');
function my_custom_tinymce_plugin_add_locale($locales) {
$locales ['My-Custom-Tinymce-Plugin'] = plugin_dir_path ( __FILE__ ) . 'my-custom-tinymce-plugin-langs.php';
return $locales;
}注意事项
- 翻译文件必须定义名为 $strings 的全局变量,否则无法正常工作。
- 从 WordPress 5.3.0 开始,过滤器新增 $editor_id 参数,需在实现时考虑兼容性。
- 确保插件标识符和文件路径正确,避免翻译加载失败。
原文内容
Filters the translations loaded for external TinyMCE 3.x plugins.
Description
The filter 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 wp_mce_translation(), and should define a variable ($strings) that holds all translated strings.
Parameters
$translationsarray-
Translations for external TinyMCE plugins.
$editor_idstring-
Unique editor identifier, e.g.
'content'.
Source
$mce_external_languages = apply_filters( 'mce_external_languages', array(), $editor_id );
Skip to note 2 content
Steven Lin
Example Migrated from Codex:
add_filter('mce_external_languages', 'my_custom_tinymce_plugin_add_locale'); function my_custom_tinymce_plugin_add_locale($locales) { $locales ['My-Custom-Tinymce-Plugin'] = plugin_dir_path ( __FILE__ ) . 'my-custom-tinymce-plugin-langs.php'; return $locales; }Replace My-Custom-Tinymce-Plugin with your identifier of your plugin and make sure the path to
my-custom-tinymce-plugin-langs.phpis correct.Translating strings with
mce_external_languagesCreate a new file, called something like
my-custom-tinymce-plugin-langs.phpand open it. Insert the following code.// This file is based on wp-includes/js/tinymce/langs/wp-langs.php if ( ! defined( 'ABSPATH' ) ) exit; if ( ! class_exists( '_WP_Editors' ) ) require( ABSPATH . WPINC . '/class-wp-editor.php' ); function my_custom_tinymce_plugin_translation() { $strings = array( 'somestring' => __('My custom Tinymce plugin', 'textdomain'), ); $locale = _WP_Editors::$mce_locale; $translated = 'tinyMCE.addI18n("' . $locale . '.my_custom_tinymce_plugin", ' . json_encode( $strings ) . ");n"; return $translated; } $strings = my_custom_tinymce_plugin_translation();What the code does
The code first checks if the file is included by WordPress. If not, it exits. Next, it checks if the class
_WP_Editorsexists. If not, the class is loaded (from wp-includes/class-wp-editor.php).We wrap the translation in a function (this to make sure there are no global variables) called
my_custom_tinymce_plugin_translation(make sure your function is unique). This is where you can translate your strings with a associative array. The key in this array is also the key you will use later on to get the translation. Then we retrieve the locale for the editor and we build some JavaScript. We use thetinyMCE.addI18nJavaScript function to add the translated strings to the editor. Some information about the arguments:"' . $locale . '.my_custom_tinymce_plugin"This is the “textdomain” of the translation. It should look rendered like “
en.my_custom_tinymce_plugin” (the language, in this case en, is set to the value of the variable$locale). We usemy_custom_tinymce_pluginas “textdomain”.json_encode( $strings )This converts the translated strings to JavaScript.
That bit of JavaScript code is returned by the function.
You can see that on the last line of the file, our function
my_custom_tinymce_plugin_translationis called and that the translated strings are saved in the global variable$strings(The variable has to be called$strings, or it won’t work).Load the translations
Next, use the next code to make sure WordPress loads the translations
unction my_custom_tinymce_plugin_add_locale($locales) { $locales ['My-Custom-Tinymce-Plugin'] = plugin_dir_path ( __FILE__ ) . 'my-custom-tinymce-plugin-langs.php'; return $locales; } add_filter('mce_external_languages', 'my_custom_tinymce_plugin_add_locale');Use the translations in your Javascript
// ed is the current tinymce editor. ed.getLang('my_custom_tinymce_plugin.somestring');This will return the translation of “My custom Tinymce plugin”.