WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/25/2018 01:59:51 PM (2 years ago)
Author:
herregroen
Message:

I18N: Add JavaScript translation support.

Adds the wp_set_script_translations function which registers translations for a JavaScript file. This function takes a handle, domain and optionally a path and ensures JavaScript translation files are loaded if they exist.

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

File:
1 edited

Legend:

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

    r43776 r43825  
    868868        $path = get_stylesheet_directory();
    869869    return load_theme_textdomain( $domain, $path );
     870}
     871
     872/**
     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;
    870955}
    871956
Note: See TracChangeset for help on using the changeset viewer.