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.
Built from https://develop.svn.wordpress.org/branches/5.0@44232


git-svn-id: http://core.svn.wordpress.org/branches/5.0@44062 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2018-12-17 03:11:10 +00:00
parent 388c007d2e
commit c4bbb20bd9
2 changed files with 73 additions and 15 deletions

View File

@ -870,23 +870,22 @@ function load_child_theme_textdomain( $domain, $path = false ) {
} }
/** /**
* Load the script translated strings. * Loads the script translated strings.
*
* @see WP_Scripts::set_translations()
* @link https://core.trac.wordpress.org/ticket/45103
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
* *
* @since 5.0.0 * @since 5.0.0
* @since 5.0.2 Uses load_script_translations() to load translation data.
*
* @see WP_Scripts::set_translations()
* *
* @param string $handle Name of the script to register a translation domain to. * @param string $handle Name of the script to register a translation domain to.
* @param string $domain The textdomain. * @param string $domain The text domain.
* @param string $path Optional. The full file path to the directory containing translation files. * @param string $path Optional. The full file path to the directory containing translation files.
* *
* @return false|string False if the script textdomain could not be loaded, the translated strings * @return false|string False if the script textdomain could not be loaded, the translated strings
* in JSON encoding otherwise. * in JSON encoding otherwise.
*/ */
function load_script_textdomain( $handle, $domain, $path = null ) { function load_script_textdomain( $handle, $domain, $path = null ) {
global $wp_scripts; $wp_scripts = wp_scripts();
if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
return false; return false;
@ -899,7 +898,7 @@ function load_script_textdomain( $handle, $domain, $path = null ) {
$file_base = $domain === 'default' ? $locale : $domain . '-' . $locale; $file_base = $domain === 'default' ? $locale : $domain . '-' . $locale;
$handle_filename = $file_base . '-' . $handle . '.json'; $handle_filename = $file_base . '-' . $handle . '.json';
if ( $path && file_exists( $path . '/' . $handle_filename ) ) { if ( $path && file_exists( $path . '/' . $handle_filename ) ) {
return file_get_contents( $path . '/' . $handle_filename ); return load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
} }
$obj = $wp_scripts->registered[ $handle ]; $obj = $wp_scripts->registered[ $handle ];
@ -953,7 +952,7 @@ function load_script_textdomain( $handle, $domain, $path = null ) {
// If the source is not from WP. // If the source is not from WP.
if ( false === $relative ) { if ( false === $relative ) {
return false; return load_script_translations( false, $handle, $domain );
} }
// Translations are always based on the unminified filename. // Translations are always based on the unminified filename.
@ -963,13 +962,72 @@ function load_script_textdomain( $handle, $domain, $path = null ) {
$md5_filename = $file_base . '-' . md5( $relative ) . '.json'; $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
if ( $path && file_exists( $path . '/' . $md5_filename ) ) { if ( $path && file_exists( $path . '/' . $md5_filename ) ) {
return file_get_contents( $path . '/' . $md5_filename ); return load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
} }
if ( file_exists( $languages_path . '/' . $md5_filename ) ) { if ( file_exists( $languages_path . '/' . $md5_filename ) ) {
return file_get_contents( $languages_path . '/' . $md5_filename ); return load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
} }
return false; return load_script_translations( false, $handle, $domain );
}
/**
* Loads the translation data for the given script handle and text domain.
*
* @since 5.0.2
*
* @param string|false $file Path to the translation file to load. False if there isn't one.
* @param string $handle Name of the script to register a translation domain to.
* @param string $domain The text domain.
* @return string|false The JSON-encoded translated strings for the given script handle and text domain. False if there are none.
*/
function load_script_translations( $file, $handle, $domain ) {
/**
* Pre-filters script translations for the given file, script handle and text domain.
*
* Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
*
* @since 5.0.2
*
* @param string|false $translations JSON-encoded translation data. Default null.
* @param string|false $file Path to the translation file to load. False if there isn't one.
* @param string $handle Name of the script to register a translation domain to.
* @param string $domain The text domain.
*/
$translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
if ( null !== $translations ) {
return $translations;
}
/**
* Filters the file path for loading script translations for the given script handle and text domain.
*
* @since 5.0.2
*
* @param string|false $file Path to the translation file to load. False if there isn't one.
* @param string $handle Name of the script to register a translation domain to.
* @param string $domain The text domain.
*/
$file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
if ( ! $file || ! is_readable( $file ) ) {
return false;
}
$translations = file_get_contents( $file );
/**
* Filters script translations for the given file, script handle and text domain.
*
* @since 5.0.2
*
* @param string $translations JSON-encoded translation data.
* @param string $file Path to the translation file that was loaded.
* @param string $handle Name of the script to register a translation domain to.
* @param string $domain The text domain.
*/
return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
} }
/** /**
@ -1404,9 +1462,9 @@ function wp_dropdown_languages( $args = array() ) {
/** /**
* Determines whether the current locale is right-to-left (RTL). * Determines whether the current locale is right-to-left (RTL).
* *
* For more information on this and similar theme functions, check out * For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook. * Conditional Tags} article in the Theme Developer Handbook.
* *
* @since 3.0.0 * @since 3.0.0

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.0.2-alpha-44231'; $wp_version = '5.0.2-alpha-44232';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.