WordPress.org

Make WordPress Core

Changeset 43825


Ignore:
Timestamp:
10/25/2018 01:59:51 PM (13 months 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.

Location:
branches/5.0
Files:
1 added
4 edited

Legend:

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

    r43788 r43825  
    479479
    480480    /**
     481     * Register a translation textdomain.
     482     *
     483     * @since 5.0.0
     484     *
     485     * @param string $handle Name of the script to register a translation domain to.
     486     * @param string $domain The textdomain.
     487     * @param string $path   Optional. The full file path to the directory containing translation files.
     488     *
     489     * @return bool True if the textdomain was registered, false if not.
     490     */
     491    public function set_translations( $handle, $domain, $path = null ) {
     492        if ( ! isset( $this->registered[ $handle ] ) ) {
     493            return false;
     494        }
     495
     496        $json_translations = load_script_textdomain( $handle, $domain, $path );
     497
     498        if ( ! $json_translations ) {
     499            return false;
     500        }
     501
     502        /** @var \_WP_Dependency $obj */
     503        $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        );
     513    }
     514
     515    /**
    481516     * Determines script dependencies.
    482517     *
  • branches/5.0/src/wp-includes/functions.wp-scripts.php

    r38810 r43825  
    191191
    192192    return $wp_scripts->localize( $handle, $object_name, $l10n );
     193}
     194
     195/**
     196 * Register translated strings for a script.
     197 *
     198 * Works only if the script has already been added.
     199 *
     200 * @see WP_Scripts::set_translations()
     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    if ( ! wp_script_is( $handle, 'enqueued' ) ) {
     220        _doing_it_wrong( __FUNCTION__, __( 'Script translations may only be set if the script is enqueued.' ), '5.0.0' );
     221    }
     222
     223    return $wp_scripts->set_translations( $handle, $domain, $path );
    193224}
    194225
  • 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
  • branches/5.0/tests/phpunit/tests/dependencies/scripts.php

    r43819 r43825  
    764764        $expected .= "<script type='text/javascript' src='/wp-includes/js/script3.js?ver={$ver}'></script>\n";
    765765        $expected .= "<script type='text/javascript' src='/wp-includes/js/script4.js?ver={$ver}'></script>\n";
     766
     767        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     768    }
     769
     770    /**
     771     * @ticket 45103
     772     */
     773    public function test_wp_set_script_translation() {
     774        wp_register_script( 'wp-i18n', '/wp-includes/js/dist/wp-i18n.js', array(), null );
     775        wp_enqueue_script( 'test-example', '/wp-includes/js/script.js', array(), null );
     776        wp_set_script_translations( 'test-example', 'default',  __DIR__ . '/../../data/languages/' );
     777
     778        $expected  = "<script type='text/javascript' src='/wp-includes/js/dist/wp-i18n.js'></script>";
     779        $expected .= "\n<script type='text/javascript'>\n(function( translations ){" .
     780                "wp.i18n.setLocaleData( translations.locale_data, \"default\" );" .
     781            "})(" . file_get_contents( __DIR__ . '/../../data/languages/default-en_US-813e104eb47e13dd4cc5af844c618754.json') . ");\n</script>\n";
     782        $expected .= "<script type='text/javascript' src='/wp-includes/js/script.js'></script>\n";
    766783
    767784        $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
Note: See TracChangeset for help on using the changeset viewer.