WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/14/2018 05:51:31 AM (2 years ago)
Author:
pento
Message:

I18N: Add JavaScript translation support.

Adds the wp_set_script_translations() function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.

Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.

Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/tests/phpunit/tests/dependencies/scripts.php

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