Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warnings if loading translations too early #6462

Closed
wants to merge 13 commits into from
52 changes: 52 additions & 0 deletions src/wp-includes/l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' ),
'<code>' . $domain . '</code>',
'<code>after_setup_theme</code>'
),
'6.7.0'
);
}

/**
* Filters a plugin's locale.
*
Expand Down Expand Up @@ -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.' ),
'<code>' . $domain . '</code>',
'<code>after_setup_theme</code>'
),
'6.7.0'
);
}

/** This filter is documented in wp-includes/l10n.php */
$locale = apply_filters( 'plugin_locale', determine_locale(), $domain );

Expand Down Expand Up @@ -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.' ),
'<code>' . $domain . '</code>',
'<code>after_setup_theme</code>'
),
'6.7.0'
);
}

/**
* Filters a theme's locale.
*
Expand Down Expand Up @@ -1373,6 +1412,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.' ),
'<code>' . $domain . '</code>'
),
'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() );
Expand Down
Loading