Make WordPress Core

Opened 15 months ago

Closed 15 months ago

Last modified 15 months ago

#62547 closed defect (bug) (worksforme)

Notice for _load_textdomain_just_in_time despite correct use of init hook

Reported by: markhowellsmead's profile markhowellsmead Owned by: swissspidy's profile swissspidy
Milestone: Priority: normal
Severity: normal Version: 6.9
Component: I18N Keywords:
Focuses: Cc:

Description

The notice “Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the pt-must-use 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 init action or later.” is being thrown in a private plugin. The call to load_plugin_text_domain is as follows.

function ptMustUseTranslations()
{
	load_plugin_textdomain('pt-must-use', false, __DIR__ . '/languages');
}
add_action('init', 'ptMustUseTranslations');

Change History (3)

#1 @swissspidy
15 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from assigned to closed

This code alone does not trigger this notice.

You are probably calling __() somewhere else in your mu-plugin earlier than init. As per https://make.wordpress.org/core/2024/10/21/i18n-improvements-6-7/, tools like Query Monitor are a snippet like the following one can help you pinpoint the exact location:

add_action(
	'doing_it_wrong_run',
	static function ( $function_name ) {
		if ( '_load_textdomain_just_in_time' === $function_name ) {
			debug_print_backtrace();
		}
	}
);

#2 @markhowellsmead
15 months ago

That code never ran, so I guess that the notice was being generated before the actions were fired. I traced it back to a call to get_plugin_data being called on a class constructor; presumably, therefore, too early.

#3 @swissspidy
15 months ago

The get_plugin_data() case is covered in that blog post I linked to as well. Setting the translate parameter to false is an easy fix that should suffice in most cases.

Note: See TracTickets for help on using tickets.