Make WordPress Core

Changeset 44232


Ignore:
Timestamp:
12/16/2018 03:53:34 PM (6 years ago)
Author:
ocean90
Message:

I18N: Introduce load_script_translations() as a wrapper for loading and filtering translation data for JavaScript files.

  • Introduces pre_load_script_translations to short-circuit the function.
  • Introduces load_script_translation_file to filter the file path for loading script translations.
  • Introduces load_script_translations to filter the JSON-encoded translation data.

Props johnbillion, strategio, swissspidy, dimadin, ocean90.
See #45425.

File:
1 edited

Legend:

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

    r44209 r44232  
    871871
    872872/**
    873  * Load the script translated strings.
     873 * Loads the script translated strings.
     874 *
     875 * @since 5.0.0
     876 * @since 5.0.2 Uses load_script_translations() to load translation data.
    874877 *
    875878 * @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  * @since 5.0.0
    880879 *
    881880 * @param string $handle Name of the script to register a translation domain to.
    882  * @param string $domain The textdomain.
     881 * @param string $domain The text domain.
    883882 * @param string $path   Optional. The full file path to the directory containing translation files.
    884883 *
     
    887886 */
    888887function load_script_textdomain( $handle, $domain, $path = null ) {
    889     global $wp_scripts;
     888    $wp_scripts = wp_scripts();
    890889
    891890    if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
     
    900899    $handle_filename = $file_base . '-' . $handle . '.json';
    901900    if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
    902         return file_get_contents( $path . '/' . $handle_filename );
     901        return load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
    903902    }
    904903
     
    954953    // If the source is not from WP.
    955954    if ( false === $relative ) {
    956         return false;
     955        return load_script_translations( false, $handle, $domain );
    957956    }
    958957
     
    964963    $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    965964    if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
    966         return file_get_contents( $path . '/' . $md5_filename );
     965        return load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
    967966    }
    968967    if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
    969         return file_get_contents( $languages_path . '/' . $md5_filename );
    970     }
    971 
    972     return false;
     968        return load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
     969    }
     970
     971    return load_script_translations( false, $handle, $domain );
     972}
     973
     974/**
     975 * Loads the translation data for the given script handle and text domain.
     976 *
     977 * @since 5.0.2
     978 *
     979 * @param string|false $file   Path to the translation file to load. False if there isn't one.
     980 * @param string       $handle Name of the script to register a translation domain to.
     981 * @param string       $domain The text domain.
     982 * @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none.
     983 */
     984function load_script_translations( $file, $handle, $domain ) {
     985    /**
     986     * Pre-filters script translations for the given file, script handle and text domain.
     987     *
     988     * Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
     989     *
     990     * @since 5.0.2
     991     *
     992     * @param string|false $translations JSON-encoded translation data. Default null.
     993     * @param string|false $file         Path to the translation file to load. False if there isn't one.
     994     * @param string       $handle       Name of the script to register a translation domain to.
     995     * @param string       $domain       The text domain.
     996     */
     997    $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
     998
     999    if ( null !== $translations ) {
     1000        return $translations;
     1001    }
     1002
     1003    /**
     1004     * Filters the file path for loading script translations for the given script handle and text domain.
     1005     *
     1006     * @since 5.0.2
     1007     *
     1008     * @param string|false $file   Path to the translation file to load. False if there isn't one.
     1009     * @param string       $handle Name of the script to register a translation domain to.
     1010     * @param string       $domain The text domain.
     1011     */
     1012    $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
     1013
     1014    if ( ! $file || ! is_readable( $file ) ) {
     1015        return false;
     1016    }
     1017
     1018    $translations = file_get_contents( $file );
     1019
     1020    /**
     1021     * Filters script translations for the given file, script handle and text domain.
     1022     *
     1023     * @since 5.0.2
     1024     *
     1025     * @param string $translations JSON-encoded translation data.
     1026     * @param string $file         Path to the translation file that was loaded.
     1027     * @param string $handle       Name of the script to register a translation domain to.
     1028     * @param string $domain       The text domain.
     1029     */
     1030    return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
    9731031}
    9741032
     
    14051463/**
    14061464 * Determines whether the current locale is right-to-left (RTL).
    1407  * 
     1465 *
    14081466 * For more information on this and similar theme functions, check out
    1409  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 
     1467 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
    14101468 * Conditional Tags} article in the Theme Developer Handbook.
    14111469 *
Note: See TracChangeset for help on using the changeset viewer.