mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-02 03:34:33 +00:00
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. Merges [43825,43828,43859,43898] from the 5.0 branch to trunk. Props herregroen, atimmer, omarreiss, nerrad, swissspidy, ocean90, georgestephanis. Fixes #45103, #45256. git-svn-id: https://develop.svn.wordpress.org/trunk@44169 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -330,6 +330,11 @@ class WP_Scripts extends WP_Dependencies {
|
||||
return true;
|
||||
}
|
||||
|
||||
$translations = $this->print_translations( $handle, false );
|
||||
if ( $translations ) {
|
||||
$translations = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $translations );
|
||||
}
|
||||
|
||||
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
|
||||
$src = $this->base_url . $src;
|
||||
}
|
||||
@@ -345,7 +350,7 @@ class WP_Scripts extends WP_Dependencies {
|
||||
return true;
|
||||
}
|
||||
|
||||
$tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
|
||||
$tag = "{$translations}{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
|
||||
|
||||
/**
|
||||
* Filters the HTML script tag of an enqueued script.
|
||||
@@ -490,6 +495,67 @@ class WP_Scripts extends WP_Dependencies {
|
||||
return parent::set_group( $handle, $recursion, $grp );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a translation textdomain.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string $handle Name of the script to register a translation domain to.
|
||||
* @param string $domain The textdomain.
|
||||
* @param string $path Optional. The full file path to the directory containing translation files.
|
||||
*
|
||||
* @return bool True if the textdomain was registered, false if not.
|
||||
*/
|
||||
public function set_translations( $handle, $domain, $path = null ) {
|
||||
if ( ! isset( $this->registered[ $handle ] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var \_WP_Dependency $obj */
|
||||
$obj = $this->registered[ $handle ];
|
||||
|
||||
if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) {
|
||||
$obj->deps[] = 'wp-i18n';
|
||||
}
|
||||
return $obj->set_translations( $domain, $path );
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints translations set for a specific handle.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param string $handle Name of the script to add the inline script to. Must be lowercase.
|
||||
* @param bool $echo Optional. Whether to echo the script instead of just returning it.
|
||||
* Default true.
|
||||
* @return string|false Script on success, false otherwise.
|
||||
*/
|
||||
public function print_translations( $handle, $echo = true ) {
|
||||
if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$domain = $this->registered[ $handle ]->textdomain;
|
||||
$path = $this->registered[ $handle ]->translations_path;
|
||||
|
||||
$json_translations = load_script_textdomain( $handle, $domain, $path );
|
||||
|
||||
if ( ! $json_translations ) {
|
||||
// Register empty locale data object to ensure the domain still exists.
|
||||
$json_translations = '{ "locale_data": { "messages": { "": {} } } }';
|
||||
}
|
||||
|
||||
$output = '(function( translations ){' .
|
||||
'wp.i18n.setLocaleData( translations.locale_data, "' . $domain . '" );' .
|
||||
'})(' . $json_translations . ');';
|
||||
|
||||
if ( $echo ) {
|
||||
printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines script dependencies.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user