Make WordPress Core

Changeset 60728

Timestamp:
09/11/2025 10:11:40 AM (4 weeks ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r59719 r60728  
    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'.
     
    11421144 */
    11431145function 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
     
    11481153    }
    11491154
    1150     $path   = untrailingslashit( $path );
    11511155    $locale = determine_locale();
     1156
     1157    if ( ! $path ) {
     1158        $path = $wp_textdomain_registry->get( $domain, $locale );
     1159    }
     1160
     1161    $path = untrailingslashit( $path );
    11521162
    11531163    // If a path was given and the handle file exists simply return it.
  • trunk/tests/phpunit/tests/dependencies/scripts.php

    r60719 r60728  
    26292629        );
    26302630        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js' id='test-example-js'></script>\n";
     2631
     2632        $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
     2633    }
     2634
     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";
    26312698
    26322699        $this->assertEqualHTML( $expected, get_echo( 'wp_print_scripts' ) );
Note: See TracChangeset for help on using the changeset viewer.