From 48f12f72df8bb2c11570158d32e7fa2e6f5dcea6 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Mon, 30 Sep 2024 15:30:20 +0000 Subject: [PATCH] I18N: Emit warnings if loading translations too early. Some plugins and themes load translations too early, before the current user is known. This happens either explicitly or through just-in-time translation loading. If the current user (and thus their locale) is not known, WordPress might attempt to load translations in the wrong locale. This change adds `_doing_it_wrong` messages to warn about such cases. It also helps avoiding accidentally trying to load translations twice (once just-in-time and once manually). Projects triggering such a message are encourage to load translations no earlier than the `after_setup_theme` hook. Props garrett-eclipse, Kau-Boy, swissspidy, johnbillion, alanfuller. rodelgc. Fixes #44937. Built from https://develop.svn.wordpress.org/trunk@59127 git-svn-id: http://core.svn.wordpress.org/trunk@58523 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/l10n.php | 52 +++++++++++++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/wp-includes/l10n.php b/wp-includes/l10n.php index 13d307fca9..75e30fa2f1 100644 --- a/wp-includes/l10n.php +++ b/wp-includes/l10n.php @@ -999,6 +999,19 @@ 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. * @@ -1051,6 +1064,19 @@ 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 ); @@ -1094,6 +1120,19 @@ 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. * @@ -1381,6 +1420,19 @@ function _load_textdomain_just_in_time( $domain ) { if ( ! $path ) { return false; } + + if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) { + _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 . '' + ), + '6.7.0' + ); + } + // Themes with their language directory outside of WP_LANG_DIR have a different file name. $template_directory = trailingslashit( get_template_directory() ); $stylesheet_directory = trailingslashit( get_stylesheet_directory() ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 05b1796e8e..276a865cbf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-alpha-59126'; +$wp_version = '6.7-alpha-59127'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.