Make WordPress Core


Ignore:
Timestamp:
12/18/2018 11:41:35 PM (8 years ago)
Author:
SergeyBiryukov
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.
Merges [44232] to trunk.
Fixes #45425.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

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

    r44310 r44316  
    895895
    896896/**
    897  * Load the script translated strings.
     897 * Loads the script translated strings.
     898 *
     899 * @since 5.0.0
     900 * @since 5.0.2 Uses load_script_translations() to load translation data.
    898901 *
    899902 * @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
    904903 *
    905904 * @param string $handle Name of the script to register a translation domain to.
    906  * @param string $domain The textdomain.
     905 * @param string $domain The text domain.
    907906 * @param string $path   Optional. The full file path to the directory containing translation files.
    908907 *
     
    911910 */
    912911function load_script_textdomain( $handle, $domain, $path = null ) {
    913         global $wp_scripts;
     912        $wp_scripts = wp_scripts();
    914913
    915914        if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
     
    924923        $handle_filename = $file_base . '-' . $handle . '.json';
    925924        if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
    926                 return file_get_contents( $path . '/' . $handle_filename );
     925                return load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
    927926        }
    928927
     
    978977        // If the source is not from WP.
    979978        if ( false === $relative ) {
    980                 return false;
     979                return load_script_translations( false, $handle, $domain );
    981980        }
    982981
     
    988987        $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
    989988        if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
    990                 return file_get_contents( $path . '/' . $md5_filename );
     989                return load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
    991990        }
    992991        if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
    993                 return file_get_contents( $languages_path . '/' . $md5_filename );
    994         }
    995 
    996         return false;
     992                return load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
     993        }
     994
     995        return load_script_translations( false, $handle, $domain );
     996}
     997
     998/**
     999 * Loads the translation data for the given script handle and text domain.
     1000 *
     1001 * @since 5.0.2
     1002 *
     1003 * @param string|false $file   Path to the translation file to load. False if there isn't one.
     1004 * @param string       $handle Name of the script to register a translation domain to.
     1005 * @param string       $domain The text domain.
     1006 * @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none.
     1007 */
     1008function load_script_translations( $file, $handle, $domain ) {
     1009        /**
     1010         * Pre-filters script translations for the given file, script handle and text domain.
     1011         *
     1012         * Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
     1013         *
     1014         * @since 5.0.2
     1015         *
     1016         * @param string|false $translations JSON-encoded translation data. Default null.
     1017         * @param string|false $file         Path to the translation file to load. False if there isn't one.
     1018         * @param string       $handle       Name of the script to register a translation domain to.
     1019         * @param string       $domain       The text domain.
     1020         */
     1021        $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
     1022
     1023        if ( null !== $translations ) {
     1024                return $translations;
     1025        }
     1026
     1027        /**
     1028         * Filters the file path for loading script translations for the given script handle and text domain.
     1029         *
     1030         * @since 5.0.2
     1031         *
     1032         * @param string|false $file   Path to the translation file to load. False if there isn't one.
     1033         * @param string       $handle Name of the script to register a translation domain to.
     1034         * @param string       $domain The text domain.
     1035         */
     1036        $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
     1037
     1038        if ( ! $file || ! is_readable( $file ) ) {
     1039                return false;
     1040        }
     1041
     1042        $translations = file_get_contents( $file );
     1043
     1044        /**
     1045         * Filters script translations for the given file, script handle and text domain.
     1046         *
     1047         * @since 5.0.2
     1048         *
     1049         * @param string $translations JSON-encoded translation data.
     1050         * @param string $file         Path to the translation file that was loaded.
     1051         * @param string $handle       Name of the script to register a translation domain to.
     1052         * @param string $domain       The text domain.
     1053         */
     1054        return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
    9971055}
    9981056
Note: See TracChangeset for help on using the changeset viewer.