Make WordPress Core


Ignore:
Timestamp:
10/25/2018 01:59:51 PM (5 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/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
Note: See TracChangeset for help on using the changeset viewer.