Skip to content
Permalink
Browse files

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.

git-svn-id: https://develop.svn.wordpress.org/branches/5.0@44404 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
ocean90 committed Jan 6, 2019
1 parent 239d1d1 commit 2acbb2a7e4e47dad466bb1f0dad836e9376f34c0
Showing with 108 additions and 43 deletions.
  1. +8 −5 src/wp-includes/class.wp-scripts.php
  2. +100 −38 tests/phpunit/tests/dependencies/scripts.php
@@ -532,10 +532,13 @@ public function print_translations( $handle, $echo = true ) {
$json_translations = '{ "locale_data": { "messages": { "": {} } } }';
}
$output = '(function( translations ){' .
'translations.locale_data.messages[""].domain = "' . $domain . '";' .
'wp.i18n.setLocaleData( translations.locale_data.messages, "' . $domain . '" );' .
'})(' . $json_translations . ');';
$output = <<<JS
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "{$domain}", {$json_translations} );
JS;
if ( $echo ) {
printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
@@ -546,7 +549,7 @@ public function print_translations( $handle, $echo = true ) {
/**
* Determines script dependencies.
*
*
* @since 2.1.0
*
* @see WP_Dependencies::all_deps()
@@ -4,7 +4,9 @@
* @group scripts
*/
class Tests_Dependencies_Scripts extends WP_UnitTestCase {
var $old_wp_scripts;
protected $old_wp_scripts;
protected $wp_scripts_print_translations_output;
function setUp() {
parent::setUp();
@@ -13,6 +15,17 @@ function setUp() {
remove_action( 'wp_default_scripts', 'wp_default_packages' );
$GLOBALS['wp_scripts'] = new WP_Scripts();
$GLOBALS['wp_scripts']->default_version = get_bloginfo( 'version' );
$this->wp_scripts_print_translations_output = <<<JS
<script type='text/javascript'>
( function( domain, translations ) {
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
localeData[""].domain = domain;
wp.i18n.setLocaleData( localeData, domain );
} )( "__DOMAIN__", __JSON_TRANSLATIONS__ );
</script>
JS;
$this->wp_scripts_print_translations_output .= "\n";
}
function tearDown() {
@@ -775,11 +788,18 @@ public function test_wp_set_script_translations() {
wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"default\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"default\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'default',
file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@@ -793,11 +813,18 @@ public function test_wp_set_script_translations_for_plugin() {
wp_enqueue_script( 'plugin-example', '/wp-content/plugins/my-plugin/js/script.js', array(), null );
wp_set_script_translations( 'plugin-example', 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"internationalized-plugin\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-plugin\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'internationalized-plugin',
file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@@ -811,11 +838,18 @@ public function test_wp_set_script_translations_for_theme() {
wp_enqueue_script( 'theme-example', '/wp-content/themes/my-theme/js/script.js', array(), null );
wp_set_script_translations( 'theme-example', 'internationalized-theme', DIR_TESTDATA . '/languages/themes' );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"internationalized-theme\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"internationalized-theme\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ) . ");\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'internationalized-theme',
file_get_contents( DIR_TESTDATA . '/languages/themes/internationalized-theme-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-content/themes/my-theme/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@@ -829,11 +863,18 @@ public function test_wp_set_script_translations_with_handle_file() {
wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null );
wp_set_script_translations( 'script-handle', 'admin', DIR_TESTDATA . '/languages/' );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"admin\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"admin\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ) . ");\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'admin',
file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ),
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@@ -862,11 +903,18 @@ public function test_wp_set_script_translations_when_translation_file_does_not_e
wp_enqueue_script( 'test-example', '/wp-admin/js/script.js', array(), null );
wp_set_script_translations( 'test-example', 'admin', DIR_TESTDATA . '/languages/' );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"admin\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"admin\" );" .
"})({ \"locale_data\": { \"messages\": { \"\": {} } } });\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'admin',
'{ "locale_data": { "messages": { "": {} } } }',
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@@ -878,15 +926,22 @@ public function test_wp_set_script_translations_when_translation_file_does_not_e
public function test_wp_set_script_translations_after_register() {
wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
wp_register_script( 'test-example', '/wp-includes/js/script.js', array(), null );
wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
wp_set_script_translations( 'test-example', 'default', DIR_TESTDATA . '/languages' );
wp_enqueue_script( 'test-example' );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"default\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"default\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'default',
file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
$this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
@@ -898,15 +953,22 @@ public function test_wp_set_script_translations_after_register() {
public function test_wp_set_script_translations_dependency() {
wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
wp_register_script( 'test-dependency', '/wp-includes/js/script.js', array(), null );
wp_set_script_translations( 'test-dependency', 'default', DIR_TESTDATA . '/languages' );
wp_set_script_translations( 'test-dependency', 'default', DIR_TESTDATA . '/languages' );
wp_enqueue_script( 'test-example', '/wp-includes/js/script2.js', array( 'test-dependency' ), null );
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
$expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
"translations.locale_data.messages[\"\"].domain = \"default\";" .
"wp.i18n.setLocaleData( translations.locale_data.messages, \"default\" );" .
"})(" . file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ) . ");\n</script>\n";
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>\n";
$expected .= str_replace(
array(
'__DOMAIN__',
'__JSON_TRANSLATIONS__',
),
array(
'default',
file_get_contents( DIR_TESTDATA . '/languages/en_US-813e104eb47e13dd4cc5af844c618754.json' ),
),
$this->wp_scripts_print_translations_output
);
$expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
$expected .= "<script type='text/javascript' src='/wp-includes/js/script2.js'></script>\n";

0 comments on commit 2acbb2a

Please sign in to comment.
You can’t perform that action at this time.