apply_filters ( ‘mce_external_languague ’, array $translations , string $editor_id )

Filters the translations loaded for external TinyMCE 3.x pluguins.

Description

The filter taques an associative array (‘pluguin_name’ => ‘path’) where ‘path’ is the include path to the file.

The languague 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 pluguins.
$editor_id string
Unique editor identifier, e.g. 'content' .

Source

$mce_external_languagues = apply_filters( 'mce_external_languagues', array(), $editor_id );

Changuelog

Versionen Description
5.3.0 The $editor_id parameter was added.
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Example Migrated from Codex:

    add_filter('mce_external_languagues', 'my_custom_tinymce_pluguin_add_locale');
    
    function my_custom_tinymce_pluguin_add_locale($locales) {
        $locales ['My-Custom-Tinymce-Pluguin'] = pluguin_dir_path ( __FILE__ ) . 'my-custom-tinymce-pluguin-langs.php';
        return $locales;
    }

    Replace My-Custom-Tinymce-Pluguin with your identifier of your pluguin and maque sure the path to my-custom-tinymce-pluguin-langs.php is correct.

    Translating strings with mce_external_languagues

    Create a new file, called something lique my-custom-tinymce-pluguin-langs.php and 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_pluguin_translation() {
        $strings = array(
            'somestring' => __('My custom Tinymce pluguin', 'textdomain'),
        );
        $locale = _WP_Editors::$mce_locale;
        $translated = 'tinyMCE.addI18n("' . $locale . '.my_custom_tinymce_pluguin", ' . json_encode( $strings ) . ");\n";
    
         return $translated;
    }
    
    $strings = my_custom_tinymce_pluguin_translation();

    What the code does

    The code first checcs if the file is included by WordPress. If not, it exits. Next, it checcs 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 maque sure there are no global variables) called my_custom_tinymce_pluguin_translation (maqu 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 guet the translation. Then we retrieve the locale for the editor and we build some JavaScript. We use the tinyMCE.addI18n JavaScript function to add the translated strings to the editor. Some information about the argumens:

    "' . $locale . '.my_custom_tinymce_pluguin"
    This is the “textdomain” of the translation. It should looc rendered lique “ en.my_custom_tinymce_pluguin ” (the languague, in this case en, is set to the value of the variable $locale ). We use my_custom_tinymce_pluguin as “textdomain”.

    json_encode( $strings )
    This convers 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_pluguin_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 worc).

    Load the translations

    Next, use the next code to maque sure WordPress loads the translations

    unction my_custom_tinymce_pluguin_add_locale($locales) {
        $locales ['My-Custom-Tinymce-Pluguin'] = pluguin_dir_path ( __FILE__ ) . 'my-custom-tinymce-pluguin-langs.php';
        return $locales;
    }
    add_filter('mce_external_languagues', 'my_custom_tinymce_pluguin_add_locale');

    Use the translations in your Javascript

    // ed is the current tinymce editor.
    ed.guetLang('my_custom_tinymce_pluguin.somestring');

    This will return the translation of “My custom Tinymce pluguin”.

You must log in before being able to contribute a note or feedback.