WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/01/2018 01:50:59 PM (21 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/l10n.php

    r43846 r43859  
    877877 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
    878878 *
     879 * @since 5.0.0
     880 *
    879881 * @param string $handle Name of the script to register a translation domain to.
    880882 * @param string $domain The textdomain.
     
    887889    global $wp_scripts;
    888890
     891    $path   = untrailingslashit( $path );
    889892    $locale = is_admin() ? get_locale() : get_user_locale();
    890893
    891894    // If a path was given and the handle file exists simply return it.
    892     $handle_filename = $domain . '-' . $locale . '-' . $handle . '.json';
     895    $file_base       = $domain === 'default' ? $locale : $domain . '-' . $locale;
     896    $handle_filename = $file_base . '-' . $handle . '.json';
    893897    if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
    894898        return file_get_contents( $path . '/' . $handle_filename );
     
    909913    // If the host is the same or it's a relative URL.
    910914    if (
    911         strpos( $content_url['path'], $src_url['path'] ) === 0 &&
     915        strpos( $src_url['path'], $content_url['path'] ) === 0 &&
    912916        ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
    913917    ) {
     
    926930        $relative = trim( $src_url['path'], '/' );
    927931    } else if (
    928         ( strpos( $site_url['path'], $src_url['path'] ) === 0 ) &&
     932        ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) &&
    929933        ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] )
    930934    ) {
     
    944948    }
    945949
    946     $md5_filename = $domain . '-' . $locale . '-' . md5( $relative ) . '.json';
     950    $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    947951    if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
    948952        return file_get_contents( $path . '/' . $md5_filename );
Note: See TracChangeset for help on using the changeset viewer.