6

I know that I can get my translations into JavaScript doing:

    $MyTranslations = array(
        'translation1' => __("Some String 1", "MyTranslations"),
        'translation2' => __("Some String 2", "MyTranslations")
    );
    wp_localize_script( 'jquery', 'my_translations', $MyTranslations );

But looking at some of WordPress TinyMCE plugins they use calls like this:

{
title : ed.getLang('advlink.link_desc'),
...
}

How is WordPress getting their translations into getLang()? Am I supposed to do it this way or do I just use my first bit of code and access the variables directly like:

{
title : my_translations.translation1,
...
}
7

Use the filter 'mce_external_languages'. From 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());

I would just use a copy of wp-includes/js/tinymce/langs/wp-langs.php … and drop that superfluous mce_escape() in favour of the original esc_js().

Sample file:

<?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' ) ) . '"
        }
    }
)';

In your plugin you just use:

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;
}

To access the new strings in JavaScript use for example:

title : ed.getLang('extrastrings.helloworld')
  • I'm trying to follow your answer, and I don't understand how to actually call the strings, after adding my local php file... Could you help please? – Lea Cohen Jul 1 '12 at 10:34
  • @LeaCohen Not sure what you mean with call. The ed.getLang() part? – fuxia Jul 1 '12 at 11:44
  • Yes, than k you for understanding.. What parameter do I pass to ed.getLang() when I build my php file like your sample file? – Lea Cohen Jul 2 '12 at 7:06
  • 1
    @LeaCohen ed.getLang('extrastrings.helloworld') – do you mean that? :) – fuxia Jul 2 '12 at 12:41
  • Yes, thank you, that's what I meant! Thanks a lot. – Lea Cohen Jul 2 '12 at 13:37
1

Your sample file gaved me an error (undefined . before extrastrings). So i changed code:

<?php # -*- coding: utf-8 -*-

$strings = 'tinyMCE.addI18n( "' . _WP_Editors::$mce_locale . '.extrastrings", {
    title: "' . esc_js( __( 'Shortcodes', 'm7' ) ) . '",
    popup_title: "' . esc_js( __( 'Шорткоды', 'm7' ) ) . '"
} )';
  • 1
    This looks like it should be a suggested edit to an existing answer rather than a new answer? – s_ha_dum Sep 13 '13 at 23:16
  • Yes it is, i'm a n00be here and a Russian ( >D ), so forgive me, haven't seen that there's a way to add comment before you wrote :( – Maikal Sep 13 '13 at 23:31
  • With 1 rep you couldn't add a comment, but I think (I am not 100% sure) that you can always suggest an edit to either the question or an answer. The edit won't post without approval by higher ranked users though. – s_ha_dum Sep 13 '13 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.