From 8cf4e9ce6a2b03ddffe29276c011d303a5c72d26 Mon Sep 17 00:00:00 2001 From: "Dominik Schilling (ocean90)" Date: Fri, 4 Jan 2019 21:11:01 +0000 Subject: [PATCH] I18N: Make domain argument optional in `wp_set_script_translations()` / `WP_Scripts::set_translations()`. Props swissspidy. Fixes #45489. git-svn-id: https://develop.svn.wordpress.org/trunk@44395 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class.wp-scripts.php | 9 +++++---- src/wp-includes/functions.wp-scripts.php | 8 ++++---- src/wp-includes/l10n.php | 5 +++-- src/wp-includes/script-loader.php | 24 ++++++++++++------------ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/wp-includes/class.wp-scripts.php b/src/wp-includes/class.wp-scripts.php index e843233cbc..26fbd6da9b 100644 --- a/src/wp-includes/class.wp-scripts.php +++ b/src/wp-includes/class.wp-scripts.php @@ -499,14 +499,14 @@ class WP_Scripts extends WP_Dependencies { * Sets a translation textdomain. * * @since 5.0.0 + * @since 5.1.0 The `$domain` parameter was made optional. * * @param string $handle Name of the script to register a translation domain to. - * @param string $domain The textdomain. + * @param string $domain Optional. Text domain. Default 'default'. * @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. + * @return bool True if the text domain was registered, false if not. */ - public function set_translations( $handle, $domain, $path = null ) { + public function set_translations( $handle, $domain = 'default', $path = null ) { if ( ! isset( $this->registered[ $handle ] ) ) { return false; } @@ -517,6 +517,7 @@ class WP_Scripts extends WP_Dependencies { if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) { $obj->deps[] = 'wp-i18n'; } + return $obj->set_translations( $domain, $path ); } diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 5aa26cca5e..2e99067f90 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -209,14 +209,14 @@ function wp_localize_script( $handle, $object_name, $l10n ) { * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. * * @since 5.0.0 + * @since 5.1.0 The `$domain` parameter was made optional. * * @param string $handle Script handle the textdomain will be attached to. - * @param string $domain The textdomain. + * @param string $domain Optional. Text domain. Default 'default'. * @param string $path Optional. The full file path to the directory containing translation files. - * - * @return bool True if the textdomain was successfully localized, false otherwise. + * @return bool True if the text domain was successfully localized, false otherwise. */ -function wp_set_script_translations( $handle, $domain, $path = null ) { +function wp_set_script_translations( $handle, $domain = 'default', $path = null ) { global $wp_scripts; if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php index 7dd86607ec..fb88ba5e07 100644 --- a/src/wp-includes/l10n.php +++ b/src/wp-includes/l10n.php @@ -898,17 +898,18 @@ function load_child_theme_textdomain( $domain, $path = false ) { * * @since 5.0.0 * @since 5.0.2 Uses load_script_translations() to load translation data. + * @since 5.1.0 The `$domain` parameter was made optional. * * @see WP_Scripts::set_translations() * * @param string $handle Name of the script to register a translation domain to. - * @param string $domain The text domain. + * @param string $domain Optional. Text domain. Default 'default'. * @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 * in JSON encoding otherwise. */ -function load_script_textdomain( $handle, $domain, $path = null ) { +function load_script_textdomain( $handle, $domain = 'default', $path = null ) { $wp_scripts = wp_scripts(); if ( ! isset( $wp_scripts->registered[ $handle ] ) ) { diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 7f745acffd..bc21f9cbac 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -466,16 +466,16 @@ function wp_default_packages_scripts( &$scripts ) { ); $package_translations = array( - 'api-fetch' => 'default', - 'blocks' => 'default', - 'block-library' => 'default', - 'components' => 'default', - 'edit-post' => 'default', - 'editor' => 'default', - 'format-library' => 'default', - 'keycodes' => 'default', - 'list-reusable-blocks' => 'default', - 'nux' => 'default', + 'api-fetch', + 'blocks', + 'block-library', + 'components', + 'edit-post', + 'editor', + 'format-library', + 'keycodes', + 'list-reusable-blocks', + 'nux', ); foreach ( $packages_dependencies as $package => $dependencies ) { @@ -485,8 +485,8 @@ function wp_default_packages_scripts( &$scripts ) { $scripts->add( $handle, $path, $dependencies, $version, 1 ); - if ( isset( $package_translations[ $package ] ) ) { - $scripts->set_translations( $handle, $package_translations[ $package ] ); + if ( in_array( $package, $package_translations, true ) ) { + $scripts->set_translations( $handle ); } } }