Skip to content

Commit c5909c4

Browse files
committed
I18N: Use Domain Path for script translations if available.
This is a follow-up to [59461] from 6.8, which leveraged `Domain Path` for just-in-time loading of MO/PHP translation files. Now, the same is done for script translations, which means plugins/themes shipping with their own translation no longer need to manually specify the translation path when calling `wp_set_script_translations()`. Props tusharbharti, swissspidy, shailu25, jsnajdr. See #62244, #54797. Fixes #63944. git-svn-id: https://develop.svn.wordpress.org/trunk@60728 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1ebc4c5 commit c5909c4

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/wp-includes/l10n.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,22 +1134,32 @@ function load_child_theme_textdomain( $domain, $path = false ) {
11341134
*
11351135
* @see WP_Scripts::set_translations()
11361136
*
1137+
* @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1138+
*
11371139
* @param string $handle Name of the script to register a translation domain to.
11381140
* @param string $domain Optional. Text domain. Default 'default'.
11391141
* @param string $path Optional. The full file path to the directory containing translation files.
11401142
* @return string|false The translated strings in JSON encoding on success,
11411143
* false if the script textdomain could not be loaded.
11421144
*/
11431145
function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
1146+
/** @var WP_Textdomain_Registry $wp_textdomain_registry */
1147+
global $wp_textdomain_registry;
1148+
11441149
$wp_scripts = wp_scripts();
11451150

11461151
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
11471152
return false;
11481153
}
11491154

1150-
$path = untrailingslashit( $path );
11511155
$locale = determine_locale();
11521156

1157+
if ( ! $path ) {
1158+
$path = $wp_textdomain_registry->get( $domain, $locale );
1159+
}
1160+
1161+
$path = untrailingslashit( $path );
1162+
11531163
// If a path was given and the handle file exists simply return it.
11541164
$file_base = 'default' === $domain ? $locale : $domain . '-' . $locale;
11551165
$handle_filename = $file_base . '-' . $handle . '.json';

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,73 @@ public function test_wp_set_script_translations() {
26322632
$this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
26332633
}
26342634

2635+
/**
2636+
* @ticket 63944
2637+
*/
2638+
public function test_wp_set_script_translations_uses_registered_domainpath_for_plugin() {
2639+
global $wp_textdomain_registry;
2640+
2641+
wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
2642+
wp_enqueue_script( 'domain-path-plugin', '/wp-content/plugins/my-plugin/js/script.js', array(), null );
2643+
2644+
// Simulate a plugin declaring DomainPath: /languages by registering a custom path.
2645+
$wp_textdomain_registry->set_custom_path( 'internationalized-plugin', DIR_TESTDATA . '/languages/plugins' );
2646+
wp_set_script_translations( 'domain-path-plugin', 'internationalized-plugin' );
2647+
2648+
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
2649+
$expected .= str_replace(
2650+
array(
2651+
'__DOMAIN__',
2652+
'__HANDLE__',
2653+
'__JSON_TRANSLATIONS__',
2654+
),
2655+
array(
2656+
'internationalized-plugin',
2657+
'domain-path-plugin',
2658+
file_get_contents( DIR_TESTDATA . '/languages/plugins/internationalized-plugin-en_US-2f86cb96a0233e7cb3b6f03ad573be0b.json' ),
2659+
),
2660+
$this->wp_scripts_print_translations_output
2661+
);
2662+
$expected .= "<script type='text/javascript' src='/wp-content/plugins/my-plugin/js/script.js' id='domain-path-plugin-js'></script>\n";
2663+
2664+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
2665+
}
2666+
2667+
/**
2668+
* @ticket 63944
2669+
*
2670+
* Ensure human-readable script translation filenames are found when a
2671+
* textdomain has a custom DomainPath registered and no explicit $path is passed.
2672+
*/
2673+
public function test_wp_set_script_translations_prefers_human_readable_filename_in_registered_domainpath() {
2674+
global $wp_textdomain_registry;
2675+
2676+
wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
2677+
wp_enqueue_script( 'script-handle', '/wp-admin/js/script.js', array(), null );
2678+
2679+
// Register the admin textdomain path and use the admin translations file for this script-handle test.
2680+
$wp_textdomain_registry->set_custom_path( 'admin', DIR_TESTDATA . '/languages' );
2681+
wp_set_script_translations( 'script-handle', 'admin' );
2682+
2683+
$expected = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js' id='wp-i18n-js'></script>\n";
2684+
$expected .= str_replace(
2685+
array(
2686+
'__DOMAIN__',
2687+
'__HANDLE__',
2688+
'__JSON_TRANSLATIONS__',
2689+
),
2690+
array(
2691+
'admin',
2692+
'script-handle',
2693+
file_get_contents( DIR_TESTDATA . '/languages/admin-en_US-script-handle.json' ),
2694+
),
2695+
$this->wp_scripts_print_translations_output
2696+
);
2697+
$expected .= "<script type='text/javascript' src='/wp-admin/js/script.js' id='script-handle-js'></script>\n";
2698+
2699+
$this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
2700+
}
2701+
26352702
/**
26362703
* @ticket 45103
26372704
*/

0 commit comments

Comments
 (0)