WordPress.org

Make WordPress Core

Changeset 44404


Ignore:
Timestamp:
01/06/2019 05:05:24 PM (10 months ago)
Author:
ocean90
Message:

I18N/Script Loader: Support text domains other than "messages".

The inline JavaScript added by WP_Scripts::print_translations() should check whether locale_data.$text_domain exists and fall back to locale_data.messages otherwise.

Merge of [44403] to the 5.0 branch.

Props swissspidy.
Fixes #45441.

Location:
branches/5.0
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/src/wp-includes/class.wp-scripts.php

    r43898 r44404  
    533533        }
    534534
    535         $output = '(function( translations ){' .
    536                       'translations.locale_data.messages[""].domain = "' . $domain . '";' .
    537                       'wp.i18n.setLocaleData( translations.locale_data.messages, "' . $domain . '" );' .
    538                   '})(' . $json_translations . ');';
     535        $output = <<<JS
     536( function( domain, translations ) {
     537    var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
     538    localeData[""].domain = domain;
     539    wp.i18n.setLocaleData( localeData, domain );
     540} )( "{$domain}", {$json_translations} );
     541JS;
    539542
    540543        if ( $echo ) {
     
    547550    /**
    548551     * Determines script dependencies.
    549     *
     552    *
    550553     * @since 2.1.0
    551554     *
  • branches/5.0/tests/phpunit/tests/dependencies/scripts.php

    r43898 r44404  
    55 */
    66class Tests_Dependencies_Scripts extends WP_UnitTestCase {
    7     var $old_wp_scripts;
     7    protected $old_wp_scripts;
     8
     9    protected $wp_scripts_print_translations_output;
    810
    911    function setUp() {
     
    1416        $GLOBALS['wp_scripts'] = new WP_Scripts();
    1517        $GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
     18
     19        $this->wp_scripts_print_translations_output  = <<<JS
     20<script type='text/javascript'>
     21( function( domain, translations ) {
     22    var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
     23    localeData[""].domain = domain;
     24    wp.i18n.setLocaleData( localeData, domain );
     25} )( "__DOMAIN__", __JSON_TRANSLATIONS__ );
     26</script>
     27JS;
     28        $this->wp_scripts_print_translations_output .= "\n";
    1629    }
    1730
     
    776789        wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages' );
    777790
    778         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    779         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    780                      "translations.locale_data.messages[\"\"].domain = \"default\";" .
    781                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"default\" );" .
    782                      "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     791        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     792        $expected .= str_replace(
     793            array(
     794                '__DOMAIN__',
     795                '__JSON_TRANSLATIONS__',
     796            ),
     797            array(
     798                'default',
     799                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
     800            ),
     801            $this->wp_scripts_print_translations_output
     802        );
    783803        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    784804
     
    794814        wp_set_script_translations( 'plugin-example', 'internationalized-plugin',  DIR_TESTDATA . '/languages/plugins' );
    795815
    796         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    797         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    798                      "translations.locale_data.messages[\"\"].domain = \"internationalized-plugin\";" .
    799                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-plugin\" );" .
    800                      "})(" . file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
     816        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     817        $expected .= str_replace(
     818            array(
     819                '__DOMAIN__',
     820                '__JSON_TRANSLATIONS__',
     821            ),
     822            array(
     823                'internationalized-plugin',
     824                file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
     825            ),
     826            $this->wp_scripts_print_translations_output
     827        );
    801828        $expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n";
    802829
     
    812839        wp_set_script_translations( 'theme-example', 'internationalized-theme',  DIR_TESTDATA . '/languages/themes' );
    813840
    814         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    815         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    816                      "translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" .
    817                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" .
    818                      "})(" . file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
     841        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     842        $expected .= str_replace(
     843            array(
     844                '__DOMAIN__',
     845                '__JSON_TRANSLATIONS__',
     846            ),
     847            array(
     848                'internationalized-theme',
     849                file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
     850            ),
     851            $this->wp_scripts_print_translations_output
     852        );
    819853        $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
    820854
     
    830864        wp_set_script_translations( 'script-handle', 'admin',  DIR_TESTDATA . '/languages/' );
    831865
    832         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    833         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    834                      "translations.locale_data.messages[\"\"].domain = \"admin\";" .
    835                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"admin\" );" .
    836                      "})(" . file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ) . ");\n</script>\n";
     866        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     867        $expected .= str_replace(
     868            array(
     869                '__DOMAIN__',
     870                '__JSON_TRANSLATIONS__',
     871            ),
     872            array(
     873                'admin',
     874                file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ),
     875            ),
     876            $this->wp_scripts_print_translations_output
     877        );
    837878        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
    838879
     
    863904        wp_set_script_translations( 'test-example', 'admin',  DIR_TESTDATA . '/languages/' );
    864905
    865         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    866         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    867                      "translations.locale_data.messages[\"\"].domain = \"admin\";" .
    868                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"admin\" );" .
    869                      "})({ \"locale_data\": { \"messages\": { \"\": {} } } });\n</script>\n";
     906        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     907        $expected .= str_replace(
     908            array(
     909                '__DOMAIN__',
     910                '__JSON_TRANSLATIONS__',
     911            ),
     912            array(
     913                'admin',
     914                '{ "locale_data": { "messages": { "": {} } } }',
     915            ),
     916            $this->wp_scripts_print_translations_output
     917        );
    870918        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
    871919
     
    879927        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    880928        wp_register_script( 'test-example', '/wp-includes/js/script.js', array(), null );
    881         wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages' );
     929        wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
    882930
    883931        wp_enqueue_script( 'test-example' );
    884932
    885         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    886         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    887                      "translations.locale_data.messages[\"\"].domain = \"default\";" .
    888                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"default\" );" .
    889                      "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     933        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     934        $expected .= str_replace(
     935            array(
     936                '__DOMAIN__',
     937                '__JSON_TRANSLATIONS__',
     938            ),
     939            array(
     940                'default',
     941                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
     942            ),
     943            $this->wp_scripts_print_translations_output
     944        );
    890945        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    891946
     
    899954        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    900955        wp_register_script( 'test-dependency', '/wp-includes/js/script.js', array(), null );
    901         wp_set_script_translations( 'test-dependency', 'default',  DIR_TESTDATA . '/languages' );
     956        wp_set_script_translations( 'test-dependency', 'default', DIR_TESTDATA . '/languages' );
    902957
    903958        wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
    904959
    905         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    906         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    907                      "translations.locale_data.messages[\"\"].domain = \"default\";" .
    908                      "wp.i18n.setLocaleData( translations.locale_data.messages, \"default\" );" .
    909                      "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     960        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     961        $expected .= str_replace(
     962            array(
     963                '__DOMAIN__',
     964                '__JSON_TRANSLATIONS__',
     965            ),
     966            array(
     967                'default',
     968                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
     969            ),
     970            $this->wp_scripts_print_translations_output
     971        );
    910972        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    911973        $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js'></script>\n";
Note: See TracChangeset for help on using the changeset viewer.