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
$translations
array- Translations for external TinyMCE plugins.
$editor_id
string- Unique editor identifier, e.g.
'content'
.
Source
$mce_external_languages = apply_filters( 'mce_external_languages', array(), $editor_id );
Example Migrated from Codex:
Replace My-Custom-Tinymce-Plugin with your identifier of your plugin and make sure the path to
my-custom-tinymce-plugin-langs.php
is correct.Translating strings with
mce_external_languages
Create a new file, called something like
my-custom-tinymce-plugin-langs.php
and open it. Insert the following code.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_Editors
exists. 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.addI18n
JavaScript 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_plugin
as “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_translation
is 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
Use the translations in your Javascript
This will return the translation of “My custom Tinymce plugin”.