WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/01/2018 01:50:59 PM (23 months ago)
Author:
herregroen
Message:

I18N: Improve JavaScript translation support.

Improves upon [43825] by adding unit tests to cover additional use-cases and changes loading translations to only occur when outputting the script to allow setting translations on dependencies.

Props atimmer, omarreiss, nerrad, swissspidy, ocean90.
Fixes #45103.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/class.wp-scripts.php

    r43825 r43859  
    326326        }
    327327
     328        $translations = $this->print_translations( $handle, false );
     329        if ( $translations ) {
     330            $translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
     331        }
     332
    328333        if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
    329334            $src = $this->base_url . $src;
     
    339344            return true;
    340345
    341         $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
     346        $tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
    342347
    343348        /**
     
    479484
    480485    /**
    481      * Register a translation textdomain.
     486     * Sets a translation textdomain.
    482487     *
    483488     * @since 5.0.0
     
    494499        }
    495500
    496         $json_translations = load_script_textdomain( $handle, $domain, $path );
    497 
    498         if ( ! $json_translations ) {
    499             return false;
    500         }
    501 
    502501        /** @var \_WP_Dependency $obj */
    503502        $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         );
     503
     504        if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
     505            $obj->deps[] = 'wp-i18n';
     506        }
     507        return $obj->set_translations( $domain, $path );
     508    }
     509
     510    /**
     511     * Prints translations set for a specific handle.
     512     *
     513     * @since 5.0.0
     514     *
     515     * @param string $handle Name of the script to add the inline script to. Must be lowercase.
     516     * @param bool   $echo   Optional. Whether to echo the script instead of just returning it.
     517     *                       Default true.
     518     * @return string|false Script on success, false otherwise.
     519     */
     520    public function print_translations( $handle, $echo = true ) {
     521        if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
     522            return false;
     523        }
     524
     525        $domain = $this->registered[ $handle ]->textdomain;
     526        $path   = $this->registered[ $handle ]->translations_path;
     527
     528        $json_translations = load_script_textdomain( $handle, $domain, $path );
     529
     530        if ( ! $json_translations ) {
     531            return false;
     532        }
     533
     534        $output = '(function( translations ){' .
     535                      'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
     536                  '})(' . $json_translations . ');';
     537
     538        if ( $echo ) {
     539            printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
     540        }
     541
     542        return $output;
    513543    }
    514544
Note: See TracChangeset for help on using the changeset viewer.