Make WordPress Core

Ticket #45103: 45103.6.diff

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

     
    478478        }
    479479
    480480        /**
     481         * Register a translation textdomain.
     482         *
     483         * @since 5.0.0
     484         *
     485         * @param string $handle Name of the script to register a translation domain to.
     486         * @param string $domain The textdomain.
     487         * @param string $path   Optional. The full file path to the directory containing translation files.
     488         *
     489         * @return bool True if the textdomain was registered, false if not.
     490         */
     491        public function set_translations( $handle, $domain, $path = null ) {
     492                if ( ! isset( $this->registered[ $handle ] ) ) {
     493                        return false;
     494                }
     495
     496                $json_translations = load_script_textdomain( $handle, $domain, $path );
     497
     498                if ( ! $json_translations ) {
     499                        return false;
     500                }
     501
     502                /** @var \_WP_Dependency $obj */
     503                $obj = $this->registered[ $handle ];
     504                $obj->deps[] = 'wp-i18n';
     505
     506                return $this->add_inline_script(
     507                        $handle,
     508                        '(function( translations ){' .
     509                            'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
     510                        '})(' . $json_translations . ');',
     511                        'before'
     512                );
     513        }
     514
     515        /**
    481516         * Determines script dependencies.
    482517     *
    483518         * @since 2.1.0
  • src/wp-includes/functions.wp-scripts.php

     
    193193}
    194194
    195195/**
     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
     226/**
    196227 * Remove a registered script.
    197228 *
    198229 * Note: there are intentional safeguards in place to prevent critical admin scripts,
  • src/wp-includes/l10n.php

     
    870870}
    871871
    872872/**
     873 * Load the script translated strings.
     874 *
     875 * @see WP_Scripts::set_translations()
     876 * @link https://core.trac.wordpress.org/ticket/45103
     877 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
     878 *
     879 * @param string $handle Name of the script to register a translation domain to.
     880 * @param string $domain The textdomain.
     881 * @param string $path   Optional. The full file path to the directory containing translation files.
     882 *
     883 * @return false|string False if the script textdomain could not be loaded, the translated strings
     884 *                      in JSON encoding otherwise.
     885 */
     886function load_script_textdomain( $handle, $domain, $path = null ) {
     887        global $wp_scripts;
     888
     889        $locale = is_admin() ? get_locale() : get_user_locale();
     890
     891        // If a path was given and the handle file exists simply return it.
     892        $handle_filename = $domain . '-' . $locale . '-' . $handle . '.json';
     893        if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
     894                return file_get_contents( $path . '/' . $handle_filename );
     895        }
     896
     897        $obj = $wp_scripts->registered[ $handle ];
     898
     899        /** This filter is documented in wp-includes/class.wp-scripts.php */
     900        $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     901
     902        $relative       = false;
     903        $languages_path = WP_LANG_DIR;
     904
     905        $src_url     = wp_parse_url( $src );
     906        $content_url = wp_parse_url( content_url() );
     907        $site_url    = wp_parse_url( site_url() );
     908
     909        // If the host is the same or it's a relative URL.
     910        if (
     911                strpos( $content_url['path'], $src_url['path'] ) === 0 &&
     912                ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
     913        ) {
     914                // Make the src relative the specific plugin or theme.
     915                $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
     916                $relative = explode( '/', $relative );
     917
     918                $languages_path = WP_LANG_DIR . '/' . $relative[0];
     919
     920                $relative = array_slice( $relative, 2 );
     921                $relative = implode( '/', $relative );
     922        } else if (
     923                ! isset( $site_url['path'] ) &&
     924                ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] )
     925        ) {
     926                $relative = trim( $src_url['path'], '/' );
     927        } else if (
     928                ( strpos( $site_url['path'], $src_url['path'] ) === 0 ) &&
     929                ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] )
     930        ) {
     931                // Make the src relative to the WP root.
     932                $relative = substr( $src, strlen( $site_url['path'] ) );
     933                $relative = trim( $relative, '/' );
     934        }
     935
     936        // If the source is not from WP.
     937        if ( false === $relative ) {
     938                return false;
     939        }
     940
     941        // Translations are always based on the unminified filename.
     942        if ( substr( $relative, -7 ) === '.min.js' ) {
     943                $relative = substr( $relative, 0, -7 ) . '.js';
     944        }
     945
     946        $md5_filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json';
     947        if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
     948                return file_get_contents( $path . '/' . $md5_filename );
     949        }
     950        if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
     951                return file_get_contents( $languages_path . '/' . $md5_filename );
     952        }
     953
     954        return false;
     955}
     956
     957/**
    873958 * Loads plugin and theme textdomains just-in-time.
    874959 *
    875960 * When a textdomain is encountered for the first time, we try to load
  • tests/phpunit/data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json

     
     1{
     2  "translation-revision-data": "+0000",
     3  "generator":                 "GlotPress/2.3.0-alpha",
     4  "domain":                    "messages",
     5  "locale_data":               {
     6    "messages": {
     7      "":                                                             {
     8        "domain":       "messages",
     9        "plural-forms": "n != 1",
     10        "lang":         "en-gb"
     11      },
     12      "This file is too big. Files must be less than %d KB in size.": [
     13        "This file is too big. Files must be less than %d KB in size."
     14      ],
     15      "%d Theme Update":                                              [
     16        "%d Theme Update",
     17        "%d Theme Updates"
     18      ],
     19      "password strength\u0004Medium":                                [
     20        "Medium"
     21      ],
     22      "taxonomy singular name\u0004Category":                         [
     23        "Category"
     24      ],
     25      "post type general name\u0004Pages":                            [
     26        "Pages"
     27      ]
     28    }
     29  }
     30}
  • tests/phpunit/tests/dependencies/scripts.php

     
    768768        }
    769769
    770770        /**
     771         * @ticket 45103
     772         */
     773        public function test_wp_set_script_translation() {
     774                wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     775                wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
     776                wp_set_script_translations( 'test-example', 'default',  __DIR__ . '/../../data/languages/' );
     777
     778                $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     779                $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     780                            "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
     781                        "})(" . file_get_contents( __DIR__ . '/../../data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json') . ");\n</script>\n";
     782                $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
     783
     784                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     785        }
     786
     787        /**
    771788         * Testing `wp_enqueue_code_editor` with file path.
    772789         *
    773790         * @ticket 41871