From f63068d5ba5d807fae460f5d7b1a0a9de0fa4b87 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 2 Oct 2024 13:44:17 +0000 Subject: [PATCH] I18N: Do not load translations directly in `load_*_textdomain`. In [59127], `_doing_it_wrong` warnings were added if plugins or themes load translations too early, either through a manual function call or just-in-time loading. Because many plugins and themes still manually call `load_plugin_textdomain()`, `load_theme_textdomain()` or `load_muplugin_textdomain()`, even though they don't have to anymore, that caused a lot of warnings. With this new approach, these functions merely register the translations path in the existing `WP_Textdomain_Registry` and do not immediately try to load the translations anymore. The loading is all handled by the just-in-time functionality. This way, warnings will only be emitted if triggering the just-in-time loading too early, greatly improving the developer experience and to a degree also performance. Props swissspidy, sergeybiryukov, mukesh27. See #44937. Built from https://develop.svn.wordpress.org/trunk@59157 git-svn-id: http://core.svn.wordpress.org/trunk@58552 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/l10n.php | 99 +++++------------------------------------ wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 90 deletions(-) diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 75e30fa2f1..e4acf40b38 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -983,6 +983,7 @@ function load_default_textdomain( $locale = null ) { * * @since 1.5.0 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. + * @since 6.7.0 Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism. * * @param string $domain Unique identifier for retrieving translated strings * @param string|false $deprecated Optional. Deprecated. Use the $plugin_rel_path parameter instead. @@ -999,36 +1000,6 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path return false; } - if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { - _doing_it_wrong( - __FUNCTION__, - sprintf( - /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user is already set up.' ), - '' . $domain . '', - 'after_setup_theme' - ), - '6.7.0' - ); - } - - /** - * Filters a plugin's locale. - * - * @since 3.0.0 - * - * @param string $locale The plugin's current locale. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - $locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); - - $mofile = $domain . '-' . $locale . '.mo'; - - // Try to load from the languages directory first. - if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) { - return true; - } - if ( false !== $plugin_rel_path ) { $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' ); } elseif ( false !== $deprecated ) { @@ -1040,7 +1011,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path $wp_textdomain_registry->set_custom_path( $domain, $path ); - return load_textdomain( $domain, $path . '/' . $mofile, $locale ); + return true; } /** @@ -1048,6 +1019,7 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path * * @since 3.0.0 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. + * @since 6.7.0 Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism. * * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. * @@ -1064,34 +1036,11 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { return false; } - if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { - _doing_it_wrong( - __FUNCTION__, - sprintf( - /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user is already set up.' ), - '' . $domain . '', - 'after_setup_theme' - ), - '6.7.0' - ); - } - - /** This filter is documented in wp-includes/l10n.php */ - $locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); - - $mofile = $domain . '-' . $locale . '.mo'; - - // Try to load from the languages directory first. - if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile, $locale ) ) { - return true; - } - $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' ); $wp_textdomain_registry->set_custom_path( $domain, $path ); - return load_textdomain( $domain, $path . '/' . $mofile, $locale ); + return true; } /** @@ -1104,6 +1053,7 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) { * * @since 1.5.0 * @since 4.6.0 The function now tries to load the .mo file from the languages directory first. + * @since 6.7.0 Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism. * * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry. * @@ -1120,43 +1070,13 @@ function load_theme_textdomain( $domain, $path = false ) { return false; } - if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { - _doing_it_wrong( - __FUNCTION__, - sprintf( - /* translators: 1: The text domain. 2: 'after_setup_theme'. */ - __( 'Attempted to load translations for the %1$s domain too early. Translations should be loaded after the %2$s action has fired, to ensure that the current user is already set up.' ), - '' . $domain . '', - 'after_setup_theme' - ), - '6.7.0' - ); - } - - /** - * Filters a theme's locale. - * - * @since 3.0.0 - * - * @param string $locale The theme's current locale. - * @param string $domain Text domain. Unique identifier for retrieving translated strings. - */ - $locale = apply_filters( 'theme_locale', determine_locale(), $domain ); - - $mofile = $domain . '-' . $locale . '.mo'; - - // Try to load from the languages directory first. - if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile, $locale ) ) { - return true; - } - if ( ! $path ) { $path = get_template_directory(); } $wp_textdomain_registry->set_custom_path( $domain, $path ); - return load_textdomain( $domain, $path . '/' . $locale . '.mo', $locale ); + return true; } /** @@ -1425,9 +1345,10 @@ function _load_textdomain_just_in_time( $domain ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: %s: The text domain. */ - __( 'Translation loading for the %s domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early.' ), - '' . $domain . '' + /* translators: 1: The text domain. 2: 'init'. */ + __( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the %2$s action or later.' ), + '' . $domain . '', + 'init' ), '6.7.0' ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 197a32fc49..4608e8dd42 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-beta1-59156'; +$wp_version = '6.7-beta1-59157'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.