WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/06/2019 04:22:46 PM (9 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.

Props swissspidy.
See #45441.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r44239 r44403  
    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
     
    782795        wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
    783796
    784         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    785         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    786                      'translations.locale_data.messages[""].domain = "default";' .
    787                      'wp.i18n.setLocaleData( translations.locale_data.messages, "default" );' .
    788                      '})(' . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     797        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     798        $expected .= str_replace(
     799            array(
     800                '__DOMAIN__',
     801                '__JSON_TRANSLATIONS__',
     802            ),
     803            array(
     804                'default',
     805                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
     806            ),
     807            $this->wp_scripts_print_translations_output
     808        );
    789809        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    790810
     
    800820        wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' );
    801821
    802         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    803         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    804                      'translations.locale_data.messages[""].domain = "internationalized-plugin";' .
    805                      'wp.i18n.setLocaleData( translations.locale_data.messages, "internationalized-plugin" );' .
    806                      '})(' . file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
     822        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     823        $expected .= str_replace(
     824            array(
     825                '__DOMAIN__',
     826                '__JSON_TRANSLATIONS__',
     827            ),
     828            array(
     829                'internationalized-plugin',
     830                file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
     831            ),
     832            $this->wp_scripts_print_translations_output
     833        );
    807834        $expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n";
    808835
     
    818845        wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes' );
    819846
    820         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    821         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    822                      'translations.locale_data.messages[""].domain = "internationalized-theme";' .
    823                      'wp.i18n.setLocaleData( translations.locale_data.messages, "internationalized-theme" );' .
    824                      '})(' . file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
     847        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     848        $expected .= str_replace(
     849            array(
     850                '__DOMAIN__',
     851                '__JSON_TRANSLATIONS__',
     852            ),
     853            array(
     854                'internationalized-theme',
     855                file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
     856            ),
     857            $this->wp_scripts_print_translations_output
     858        );
    825859        $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
    826860
     
    836870        wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' );
    837871
    838         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    839         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    840                      'translations.locale_data.messages[""].domain = "admin";' .
    841                      'wp.i18n.setLocaleData( translations.locale_data.messages, "admin" );' .
    842                      '})(' . file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ) . ");\n</script>\n";
     872        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     873        $expected .= str_replace(
     874            array(
     875                '__DOMAIN__',
     876                '__JSON_TRANSLATIONS__',
     877            ),
     878            array(
     879                'admin',
     880                file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ),
     881            ),
     882            $this->wp_scripts_print_translations_output
     883        );
    843884        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
    844885
     
    869910        wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' );
    870911
    871         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    872         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    873                      'translations.locale_data.messages[""].domain = "admin";' .
    874                      'wp.i18n.setLocaleData( translations.locale_data.messages, "admin" );' .
    875                      "})({ \"locale_data\": { \"messages\": { \"\": {} } } });\n</script>\n";
     912        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     913        $expected .= str_replace(
     914            array(
     915                '__DOMAIN__',
     916                '__JSON_TRANSLATIONS__',
     917            ),
     918            array(
     919                'admin',
     920                '{ "locale_data": { "messages": { "": {} } } }',
     921            ),
     922            $this->wp_scripts_print_translations_output
     923        );
    876924        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
    877925
     
    889937        wp_enqueue_script( 'test-example' );
    890938
    891         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    892         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    893                      'translations.locale_data.messages[""].domain = "default";' .
    894                      'wp.i18n.setLocaleData( translations.locale_data.messages, "default" );' .
    895                      '})(' . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     939        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     940        $expected .= str_replace(
     941            array(
     942                '__DOMAIN__',
     943                '__JSON_TRANSLATIONS__',
     944            ),
     945            array(
     946                'default',
     947                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
     948            ),
     949            $this->wp_scripts_print_translations_output
     950        );
    896951        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    897952
     
    909964        wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
    910965
    911         $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    912         $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    913                      'translations.locale_data.messages[""].domain = "default";' .
    914                      'wp.i18n.setLocaleData( translations.locale_data.messages, "default" );' .
    915                      '})(' . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     966        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
     967        $expected .= str_replace(
     968            array(
     969                '__DOMAIN__',
     970                '__JSON_TRANSLATIONS__',
     971            ),
     972            array(
     973                'default',
     974                file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
     975            ),
     976            $this->wp_scripts_print_translations_output
     977        );
    916978        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    917979        $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js'></script>\n";
Note: See TracChangeset for help on using the changeset viewer.