Make WordPress Core

Ticket #45103: 45103.5.diff

File 45103.5.diff, 7.1 KB (added by herregroen, 5 years ago)
  • src/wp-includes/class.wp-scripts.php

    diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php
    index a9299f0b91..1070b8800f 100644
    a b class WP_Scripts extends WP_Dependencies { 
    463463                return parent::set_group( $handle, $recursion, $grp );
    464464        }
    465465
     466        /**
     467         * Register a translation textdomain.
     468         *
     469         * @since 5.0.0
     470         *
     471         * @param string $handle Name of the script to register a translation domain to.
     472         * @param string $domain The textdomain.
     473         * @param string $path   Optional. The full file path to the directory containing translation files.
     474         *
     475         * @return bool True if the textdomain was registered, false if not.
     476         */
     477        public function set_translations( $handle, $domain, $path = null ) {
     478                if ( ! isset( $this->registered[ $handle ] ) ) {
     479                        return false;
     480                }
     481
     482                $json_translations = load_script_textdomain( $handle, $domain, $path );
     483
     484                if ( ! $json_translations ) {
     485                        return false;
     486                }
     487
     488                /** @var \_WP_Dependency $obj */
     489                $obj = $this->registered[ $handle ];
     490                $obj->deps[] = 'wp-i18n';
     491
     492                return $this->add_inline_script(
     493                        $handle,
     494                        '(function( translations ){' .
     495                            'wp.i18n.setLocaleData( translations.locale_data, ' . $domain . ' );' .
     496                        '})(' . $json_translations . ');',
     497                        'before'
     498                );
     499        }
     500
    466501        /**
    467502         * Determines script dependencies.
    468503     *
  • src/wp-includes/functions.wp-scripts.php

    diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php
    index dccaaf262a..d7b5d1d0d6 100644
    a b function wp_localize_script( $handle, $object_name, $l10n ) { 
    192192        return $wp_scripts->localize( $handle, $object_name, $l10n );
    193193}
    194194
     195/**
     196 * Register translated strings for a script.
     197 *
     198 * Works only if the script has already been added.
     199 *
     200 * @see WP_Scripts::set_translations()
     201 * @link https://core.trac.wordpress.org/ticket/45103
     202 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
     203 *
     204 * @since 5.0.0
     205 *
     206 * @param string $handle Script handle the textdomain will be attached to.
     207 * @param string $domain The textdomain.
     208 * @param string $path   Optional. The full file path to the directory containing translation files.
     209 *
     210 * @return bool True if the textdomain was successfully localized, false otherwise.
     211 */
     212function wp_set_script_translations( $handle, $domain, $path = null ) {
     213        global $wp_scripts;
     214        if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
     215                _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
     216                return false;
     217        }
     218
     219        if ( ! wp_script_is( $handle, 'enqueued' ) ) {
     220                _doing_it_wrong( __FUNCTION__, __( 'Script translations may only be set if the script is enqueued.' ), '5.0.0' );
     221        }
     222
     223        return $wp_scripts->set_translations( $handle, $domain, $path );
     224}
     225
    195226/**
    196227 * Remove a registered script.
    197228 *
  • src/wp-includes/l10n.php

    diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
    index a273b0c6dd..a08165ebf2 100644
    a b function load_child_theme_textdomain( $domain, $path = false ) { 
    821821        return load_theme_textdomain( $domain, $path );
    822822}
    823823
     824/**
     825 * Load the script translated strings.
     826 *
     827 * @see WP_Scripts::set_translations()
     828 * @link https://core.trac.wordpress.org/ticket/45103
     829 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
     830 *
     831 * @param string $handle Name of the script to register a translation domain to.
     832 * @param string $domain The textdomain.
     833 * @param string $path   Optional. The full file path to the directory containing translation files.
     834 *
     835 * @return false|string False if the script textdomain could not be loaded, the translated strings
     836 *                      in JSON encoding otherwise.
     837 */
     838function load_script_textdomain( $handle, $domain, $path = null ) {
     839        global $wp_scripts;
     840
     841        $locale = is_admin() ? get_locale() : get_user_locale();
     842
     843        // If a path was given and the handle file exists simply return it.
     844        $handle_filename = $domain . '-' . $locale . '-' . $handle . '.json';
     845        if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
     846                return file_get_contents( $path . '/' . $handle_filename );
     847        }
     848
     849        $obj = $wp_scripts->registered[ $handle ];
     850
     851        /** This filter is documented in wp-includes/class.wp-scripts.php */
     852        $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     853
     854        $relative       = false;
     855        $languages_path = WP_LANG_DIR;
     856
     857        $src_url     = wp_parse_url( $src );
     858        $content_url = wp_parse_url( content_url() );
     859        $site_url    = wp_parse_url( site_url() );
     860
     861        // If the host is the same or it's a relative URL.
     862        if (
     863                strpos( $content_url['path'], $src_url['path'] ) === 0 &&
     864                ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
     865        ) {
     866                // Make the src relative the specific plugin or theme.
     867                $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
     868                $relative = explode( '/', $relative );
     869
     870                $languages_path = WP_LANG_DIR . '/' . $relative[0];
     871
     872                $relative = array_slice( $relative, 2 );
     873                $relative = implode( '/', $relative );
     874        } else if (
     875                strpos( $site_url['path'], $src_url['path'] ) === 0 &&
     876                ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] )
     877        ) {
     878                // Make the src relative to the WP root.
     879                $relative = substr( $src, strlen( $site_url['path'] ) );
     880                $relative = trim( $relative, '/' );
     881        }
     882
     883        // If the source is not from WP.
     884        if ( false === $relative ) {
     885                return false;
     886        }
     887
     888        // Translations are always based on the unminified filename.
     889        if ( substr( $relative, -7 ) === '.min.js' ) {
     890                $relative = substr( $relative, 0, -7 ) . '.js';
     891        }
     892
     893        $md5_filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json';
     894        if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
     895                return file_get_contents( $path . '/' . $md5_filename );
     896        }
     897        if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
     898                return file_get_contents( $languages_path . '/' . $md5_filename );
     899        }
     900
     901        return false;
     902}
     903
    824904/**
    825905 * Loads plugin and theme textdomains just-in-time.
    826906 *
  • tests/phpunit/tests/dependencies/scripts.php

    diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php
    index 4dc248279e..63ac03aa7e 100644
    a b class Tests_Dependencies_Scripts extends WP_UnitTestCase { 
    725725                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
    726726        }
    727727
     728        /**
     729         * @ticket 45103
     730         */
     731        public function test_wp_set_script_translation() {
     732                wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
     733                wp_set_script_translations( 'test-example', 'default' );
     734
     735                $expected  = "<script type='text/javascript'>\n(function( translations ){" .
     736                            "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
     737                        "})(" . file_get_contents( __DIR__ . '../../languages/default-en_GB-447a5674463af55892a18a0773005f90.json') . ");\n</script>\n";
     738                $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     739
     740                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     741        }
     742
    728743        /**
    729744         * Testing `wp_enqueue_code_editor` with file path.
    730745         *