Make WordPress Core

Changeset 59127


Ignore:
Timestamp:
09/30/2024 03:28:26 PM (2 years ago)
Author:
swissspidy
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/l10n.php

    r59126 r59127  
    10001000        }
    10011001
     1002        if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) {
     1003                _doing_it_wrong(
     1004                        __FUNCTION__,
     1005                        sprintf(
     1006                                /* translators: 1: The text domain. 2: 'after_setup_theme'. */
     1007                                __( '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.' ),
     1008                                '<code>' . $domain . '</code>',
     1009                                '<code>after_setup_theme</code>'
     1010                        ),
     1011                        '6.7.0'
     1012                );
     1013        }
     1014
    10021015        /**
    10031016         * Filters a plugin's locale.
    … …  
    10521065        }
    10531066
     1067        if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) {
     1068                _doing_it_wrong(
     1069                        __FUNCTION__,
     1070                        sprintf(
     1071                                /* translators: 1: The text domain. 2: 'after_setup_theme'. */
     1072                                __( '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.' ),
     1073                                '<code>' . $domain . '</code>',
     1074                                '<code>after_setup_theme</code>'
     1075                        ),
     1076                        '6.7.0'
     1077                );
     1078        }
     1079
    10541080        /** This filter is documented in wp-includes/l10n.php */
    10551081        $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
    … …  
    10931119        if ( ! is_string( $domain ) ) {
    10941120                return false;
     1121        }
     1122
     1123        if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) {
     1124                _doing_it_wrong(
     1125                        __FUNCTION__,
     1126                        sprintf(
     1127                                /* translators: 1: The text domain. 2: 'after_setup_theme'. */
     1128                                __( '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.' ),
     1129                                '<code>' . $domain . '</code>',
     1130                                '<code>after_setup_theme</code>'
     1131                        ),
     1132                        '6.7.0'
     1133                );
    10951134        }
    10961135
    … …  
    13821421                return false;
    13831422        }
     1423
     1424        if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) {
     1425                _doing_it_wrong(
     1426                        __FUNCTION__,
     1427                        sprintf(
     1428                                /* translators: %s: The text domain. */
     1429                                __( '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.' ),
     1430                                '<code>' . $domain . '</code>'
     1431                        ),
     1432                        '6.7.0'
     1433                );
     1434        }
     1435
    13841436        // Themes with their language directory outside of WP_LANG_DIR have a different file name.
    13851437        $template_directory   = trailingslashit( get_template_directory() );
Note: See TracChangeset for help on using the changeset viewer.