Make WordPress Core


Ignore:
Timestamp:
12/14/2018 05:51:31 AM (6 years ago)
Author:
pento
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.

Merges [43825,43828,43859,43898] from the 5.0 branch to trunk.

Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis.
Fixes #45103, #45256.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/l10n.php

    r44134 r44169  
    892892    }
    893893    return load_theme_textdomain( $domain, $path );
     894}
     895
     896/**
     897 * Load the script translated strings.
     898 *
     899 * @see WP_Scripts::set_translations()
     900 * @link https://core.trac.wordpress.org/ticket/45103
     901 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
     902 *
     903 * @since 5.0.0
     904 *
     905 * @param string $handle Name of the script to register a translation domain to.
     906 * @param string $domain The textdomain.
     907 * @param string $path   Optional. The full file path to the directory containing translation files.
     908 *
     909 * @return false|string False if the script textdomain could not be loaded, the translated strings
     910 *                      in JSON encoding otherwise.
     911 */
     912function load_script_textdomain( $handle, $domain, $path = null ) {
     913    global $wp_scripts;
     914
     915    $path   = untrailingslashit( $path );
     916    $locale = is_admin() ? get_locale() : get_user_locale();
     917
     918    // If a path was given and the handle file exists simply return it.
     919    $file_base       = $domain === 'default' ? $locale : $domain . '-' . $locale;
     920    $handle_filename = $file_base . '-' . $handle . '.json';
     921    if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
     922        return file_get_contents( $path . '/' . $handle_filename );
     923    }
     924
     925    $obj = $wp_scripts->registered[ $handle ];
     926
     927    /** This filter is documented in wp-includes/class.wp-scripts.php */
     928    $src = esc_url( apply_filters( 'script_loader_src', $obj->src, $handle ) );
     929
     930    $relative       = false;
     931    $languages_path = WP_LANG_DIR;
     932
     933    $src_url     = wp_parse_url( $src );
     934    $content_url = wp_parse_url( content_url() );
     935    $site_url    = wp_parse_url( site_url() );
     936
     937    // If the host is the same or it's a relative URL.
     938    if (
     939        strpos( $src_url['path'], $content_url['path'] ) === 0 &&
     940        ( ! isset( $src_url['host'] ) || $src_url['host'] !== $content_url['host'] )
     941    ) {
     942        // Make the src relative the specific plugin or theme.
     943        $relative = trim( substr( $src, strlen( $content_url['path'] ) ), '/' );
     944        $relative = explode( '/', $relative );
     945
     946        $languages_path = WP_LANG_DIR . '/' . $relative[0];
     947
     948        $relative = array_slice( $relative, 2 );
     949        $relative = implode( '/', $relative );
     950    } elseif ( ! isset( $src_url['host'] ) || $src_url['host'] !== $site_url['host'] ) {
     951        if ( ! isset( $site_url['path'] ) ) {
     952            $relative = trim( $src_url['path'], '/' );
     953        } elseif ( ( strpos( $src_url['path'], $site_url['path'] ) === 0 ) ) {
     954            // Make the src relative to the WP root.
     955            $relative = substr( $src, strlen( $site_url['path'] ) );
     956            $relative = trim( $relative, '/' );
     957        }
     958    }
     959
     960    // If the source is not from WP.
     961    if ( false === $relative ) {
     962        return false;
     963    }
     964
     965    // Translations are always based on the unminified filename.
     966    if ( substr( $relative, -7 ) === '.min.js' ) {
     967        $relative = substr( $relative, 0, -7 ) . '.js';
     968    }
     969
     970    $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
     971    if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
     972        return file_get_contents( $path . '/' . $md5_filename );
     973    }
     974    if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
     975        return file_get_contents( $languages_path . '/' . $md5_filename );
     976    }
     977
     978    return false;
    894979}
    895980
Note: See TracChangeset for help on using the changeset viewer.