Make WordPress Core

Ticket #45103: 45103.diff

File 45103.diff, 6.7 KB (added by herregroen, 7 years ago)
  • src/wp-includes/class-wp-dependency.php

    diff --git a/src/wp-includes/class-wp-dependency.php b/src/wp-includes/class-wp-dependency.php
    index cd808e8ece..2976b466a2 100644
    a b class _WP_Dependency { 
    6767         */
    6868        public $extra = array();
    6969
     70        /**
     71         * Translations textdomain registered to this handle.
     72         *
     73         * @since 5.0.0
     74         * @var string
     75         */
     76        public $textdomain;
     77
     78        /**
     79         * Translations path registered to this handle.
     80         *
     81         * @since 5.0.0
     82         * @var string
     83         */
     84        public $translations_path;
     85
    7086        /**
    7187         * Setup dependencies.
    7288         *
    class _WP_Dependency { 
    94110                return true;
    95111        }
    96112
     113        /**
     114         * Register a translation domain.
     115         *
     116         * @since 5.0.0
     117         *
     118         * @param string $domain The translation domain to register.
     119         * @param string $path   Optional. The full file path to the directory containing translation files.
     120         *
     121         * @return bool False if not a string, true otherwise.
     122         */
     123        public function set_translations( $domain, $path = null ) {
     124                if ( !is_string($domain) )
     125                        return false;
     126                $this->textdomain = $domain;
     127                $this->translations_path = $path;
     128                return true;
     129        }
    97130}
  • 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..6f77e0e9ff 100644
    a b class WP_Scripts extends WP_Dependencies { 
    324324                if ( ! $src )
    325325                        return true;
    326326
    327                 $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
     327                $translations = '';
     328                if ( $obj->text_domain ) {
     329                        $translation_script = $this->print_inline_translations( $src, $obj->text_domain, get_locale(), $obj->translations_path, false );
     330
     331                        if ( $translation_script ) {
     332                                $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translation_script );
     333                        }
     334                }
     335
     336                $tag = "{$cond_before}{$translations}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
    328337
    329338                /**
    330339                 * Filters the HTML script tag of an enqueued script.
    class WP_Scripts extends WP_Dependencies { 
    400409                return $output;
    401410        }
    402411
     412        /**
     413         * Prints inline translations/
     414         *
     415         * @param string $src             The script source.
     416         * @param string $domain          The translation domain.
     417         * @param string $locale          The locale.
     418         * @param string $registered_path Optional. The registered path for the translations, if any.
     419         * @param bool   $echo            Optional. Whether to echo the script instead of just returning it.
     420         *                                Default true.
     421         *
     422         * @return false|string False if no translations could be found. The script otherwise.
     423         */
     424        public function print_inline_translations( $src, $domain, $locale, $registered_path = null, $echo = true ) {
     425                $relative          = false;
     426                $languages_path    = WP_LANG_DIR;
     427                $json_translations = false;
     428
     429                // First see if the script is from the content directory.
     430                $content_url = content_url();
     431                if ( strpos( $content_url, $src ) === 0 ) {
     432                        // Make the src relative the specific plugin or theme.
     433                        $relative = trim( substr( $src, strlen( $content_url ) ), '/' );
     434                        $relative = explode( '/', $relative );
     435
     436                        $languages_path = WP_LANG_DIR . '/' . $relative[0];
     437
     438                        $relative = array_slice( $relative, 2 );
     439                        $relative = implode( '/', $relative );
     440                } else {
     441                        // Otherwise check if it's from another WP directory.
     442                        $site_url = site_url();
     443                        if ( strpos( $site_url, $src ) === 0 ) {
     444                                // Make the src relative to the WP root.
     445                                $relative = substr( $src, strlen( $site_url ) );
     446                                $relative = trim( $relative, '/' );
     447                        }
     448                }
     449
     450                // If the source is not from WP.
     451                if ( $relative === false ) {
     452                        return false;
     453                }
     454
     455                $filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json';
     456
     457                // If a registered path was given check if a translation exists.
     458                if ( $registered_path && file_exists( $registered_path . '/' . $filename ) ) {
     459                        $json_translations = file_get_contents( $registered_path . '/' . $filename );
     460                }
     461
     462                // Otherwise check if the translation exists in the languages path.
     463                if ( ! $json_translations && file_exists( $languages_path . '/' . $filename ) ) {
     464                        $json_translations = file_get_contents( $languages_path . '/' . $filename );
     465                }
     466
     467                if ( ! $json_translations ) {
     468                        return false;
     469                }
     470
     471                $output = 'wp.i18n.setLocaleData( ' . $json_translations . ', ' . $domain . ' );';
     472
     473                if ( $echo ) {
     474                        printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
     475                }
     476
     477                return $output;
     478        }
     479
    403480        /**
    404481         * Localizes a script, only if the script has already been added.
    405482         *
    class WP_Scripts extends WP_Dependencies { 
    463540                return parent::set_group( $handle, $recursion, $grp );
    464541        }
    465542
     543        /**
     544         * Register a translation textdomain.
     545         *
     546         * @since 5.0.0
     547         *
     548         * @param string $handle Name of the script to register a translation domain to.
     549         * @param string $domain The textdomain.
     550         * @param string $path   Optional. The full file path to the directory containing translation files.
     551         *
     552         * @return bool True if the textdomain was registered, false if not.
     553         */
     554        public function set_translations( $handle, $domain, $path = null ) {
     555                if ( ! isset( $this->registered[ $handle ] ) ) {
     556                        return false;
     557                }
     558
     559                return $this->registered[ $handle ]->set_translations( $domain, $path );
     560        }
     561
    466562        /**
    467563         * Determines script dependencies.
    468564     *
  • 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..dee66014d9 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::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 *