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
This commit is contained in:
Pascal Birchler 2024-09-30 15:30:20 +00:00
parent ed16539779
commit 48f12f72df
2 changed files with 53 additions and 1 deletions

View File

@ -999,6 +999,19 @@ function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path
return 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.' ),
'<code>' . $domain . '</code>',
'<code>after_setup_theme</code>'
),
'6.7.0'
);
}
/** /**
* Filters a plugin's locale. * Filters a plugin's locale.
* *
@ -1051,6 +1064,19 @@ function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
return 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.' ),
'<code>' . $domain . '</code>',
'<code>after_setup_theme</code>'
),
'6.7.0'
);
}
/** This filter is documented in wp-includes/l10n.php */ /** This filter is documented in wp-includes/l10n.php */
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain ); $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
@ -1094,6 +1120,19 @@ function load_theme_textdomain( $domain, $path = false ) {
return 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.' ),
'<code>' . $domain . '</code>',
'<code>after_setup_theme</code>'
),
'6.7.0'
);
}
/** /**
* Filters a theme's locale. * Filters a theme's locale.
* *
@ -1381,6 +1420,19 @@ function _load_textdomain_just_in_time( $domain ) {
if ( ! $path ) { if ( ! $path ) {
return false; 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.' ),
'<code>' . $domain . '</code>'
),
'6.7.0'
);
}
// Themes with their language directory outside of WP_LANG_DIR have a different file name. // Themes with their language directory outside of WP_LANG_DIR have a different file name.
$template_directory = trailingslashit( get_template_directory() ); $template_directory = trailingslashit( get_template_directory() );
$stylesheet_directory = trailingslashit( get_stylesheet_directory() ); $stylesheet_directory = trailingslashit( get_stylesheet_directory() );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.