Make WordPress Core

Ticket #45103: 45103.2.diff

File 45103.2.diff, 4.1 KB (added by swissspidy, 7 years ago)
  • src/wp-includes/class.wp-scripts.php

    diff --git src/wp-includes/class.wp-scripts.php src/wp-includes/class.wp-scripts.php
    index a9299f0b91..873985cc9e 100644
    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                /** @var \_WP_Dependency $obj */
     483                $obj = $this->registered[ $handle ];
     484
     485                /** This filter is documented in wp-includes/class.wp-scripts.php */
     486                $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     487
     488                $relative          = false;
     489                $languages_path    = WP_LANG_DIR;
     490                $json_translations = false;
     491
     492                // First see if the script is from the content directory.
     493                $content_url = content_url();
     494                if ( strpos( $content_url, $src ) === 0 ) {
     495                        // Make the src relative the specific plugin or theme.
     496                        $relative = trim( substr( $src, strlen( $content_url ) ), '/' );
     497                        $relative = explode( '/', $relative );
     498
     499                        $languages_path = WP_LANG_DIR . '/' . $relative[0];
     500
     501                        $relative = array_slice( $relative, 2 );
     502                        $relative = implode( '/', $relative );
     503                } else {
     504                        // Otherwise check if it's from another WP directory.
     505                        $site_url = site_url();
     506                        if ( strpos( $site_url, $src ) === 0 ) {
     507                                // Make the src relative to the WP root.
     508                                $relative = substr( $src, strlen( $site_url ) );
     509                                $relative = trim( $relative, '/' );
     510                        }
     511                }
     512
     513                // If the source is not from WP.
     514                if ( false === $relative ) {
     515                        return false;
     516                }
     517
     518                $locale = is_admin() ? get_locale() : get_user_locale();
     519
     520                $filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json';
     521
     522                // If a registered path was given check if a translation exists.
     523                if ( $path && file_exists( $path . '/' . $filename ) ) {
     524                        $json_translations = file_get_contents( $path . '/' . $filename );
     525                }
     526
     527                // Otherwise check if the translation exists in the languages path.
     528                if ( ! $json_translations && file_exists( $languages_path . '/' . $filename ) ) {
     529                        $json_translations = file_get_contents( $languages_path . '/' . $filename );
     530                }
     531
     532                if ( ! $json_translations ) {
     533                        return false;
     534                }
     535
     536                $obj->deps[] = 'wp-i18n';
     537
     538                return $this->add_inline_script( $handle, 'wp.i18n.setLocaleData( ' . $json_translations . ', ' . $domain . ' );', 'before' );
     539        }
     540
    466541        /**
    467542         * Determines script dependencies.
    468543     *
  • src/wp-includes/functions.wp-scripts.php

    diff --git src/wp-includes/functions.wp-scripts.php src/wp-includes/functions.wp-scripts.php
    index dccaaf262a..dee66014d9 100644
    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::register_textdomain()
     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        return $wp_scripts->set_translations( $handle, $domain, $path );
     220}
     221
    195222/**
    196223 * Remove a registered script.
    197224 *