WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/01/2018 01:50:59 PM (2 years ago)
Author:
herregroen
Message:

I18N: Improve JavaScript translation support.

Improves upon [43825] by adding unit tests to cover additional use-cases and changes loading translations to only occur when outputting the script to allow setting translations on dependencies.

Props atimmer, omarreiss, nerrad, swissspidy, ocean90.
Fixes #45103.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/tests/phpunit/tests/dependencies/scripts.php

    r43828 r43859  
    771771     * @ticket 45103
    772772     */
    773     public function test_wp_set_script_translation() {
     773    public function test_wp_set_script_translations() {
     774        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     775        wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
     776        wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages' );
     777
     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                "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
     781            "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     782        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     783
     784        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     785    }
     786
     787    /**
     788     * @ticket 45103
     789     */
     790    public function test_wp_set_script_translations_for_plugin() {
     791        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     792        wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null );
     793        wp_set_script_translations( 'plugin-example', 'internationalized-plugin',  DIR_TESTDATA . '/languages/plugins' );
     794
     795        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     796        $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     797                     "wp.i18n.setLocaleData( translations.locale_data, \"internationalized-plugin\" );" .
     798                     "})(" . file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
     799        $expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n";
     800
     801        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     802    }
     803
     804    /**
     805     * @ticket 45103
     806     */
     807    public function test_wp_set_script_translations_for_theme() {
     808        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     809        wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null );
     810        wp_set_script_translations( 'theme-example', 'internationalized-theme',  DIR_TESTDATA . '/languages/themes' );
     811
     812        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     813        $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     814                     "wp.i18n.setLocaleData( translations.locale_data, \"internationalized-theme\" );" .
     815                     "})(" . file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
     816        $expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
     817
     818        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     819    }
     820
     821    /**
     822     * @ticket 45103
     823     */
     824    public function test_wp_set_script_translations_with_handle_file() {
     825        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     826        wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null );
     827        wp_set_script_translations( 'script-handle', 'admin',  DIR_TESTDATA . '/languages/' );
     828
     829        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     830        $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     831                     "wp.i18n.setLocaleData( translations.locale_data, \"admin\" );" .
     832                     "})(" . file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ) . ");\n</script>\n";
     833        $expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
     834
     835        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     836    }
     837
     838    /**
     839     * @ticket 45103
     840     */
     841    public function test_wp_set_script_translations_i18n_dependency() {
     842        global $wp_scripts;
     843
    774844        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
    775845        wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
    776846        wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages/' );
    777847
     848        $script = $wp_scripts->registered[ 'test-example' ];
     849
     850        $this->assertContains( 'wp-i18n', $script->deps );
     851    }
     852
     853    /**
     854     * @ticket 45103
     855     */
     856    public function test_wp_set_script_translations_when_translation_file_does_not_exist() {
     857        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     858        wp_enqueue_script( 'test-example', '/wp-admin/js/script.js', array(), null );
     859        wp_set_script_translations( 'test-example', 'admin',  DIR_TESTDATA . '/languages/' );
     860
     861        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     862        $expected .= "\n<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
     863
     864        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     865    }
     866
     867    /**
     868     * @ticket 45103
     869     */
     870    public function test_wp_set_script_translations_after_register() {
     871        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     872        wp_register_script( 'test-example', '/wp-includes/js/script.js', array(), null );
     873        wp_set_script_translations( 'test-example', 'default',  DIR_TESTDATA . '/languages' );
     874
     875        wp_enqueue_script( 'test-example' );
     876
    778877        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
    779878        $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
    780                 "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
    781             "})(" . file_get_contents( DIR_TESTDATA . '/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json') . ");\n</script>\n";
     879                     "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
     880                     "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
    782881        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     882
     883        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     884    }
     885
     886    /**
     887     * @ticket 45103
     888     */
     889    public function test_wp_set_script_translations_dependency() {
     890        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     891        wp_register_script( 'test-dependency', '/wp-includes/js/script.js', array(), null );
     892        wp_set_script_translations( 'test-dependency', 'default',  DIR_TESTDATA . '/languages' );
     893
     894        wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
     895
     896        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     897        $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     898                     "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
     899                     "})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
     900        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     901        $expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js'></script>\n";
    783902
    784903        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
Note: See TracChangeset for help on using the changeset viewer.