Make WordPress Core

Opened 12 months ago

Closed 3 months ago

#58546 closed enhancement (duplicate)

Warn if _load_textdomain_just_in_time() is used before init

Reported by: swissspidy's profile swissspidy Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: I18N Keywords: dev-feedback
Focuses: Cc:

Description

Whenever a translation function like __() is used for the first time with a specific domain, _load_textdomain_just_in_time() kicks in to load the plugin's/theme's translations automatically.

This function calls determine_locale() to load the translations in the current user's locale if available.

If the JIT loading happens before the current user is set, it will load the translations in the wrong locale.

Such situations were previously reported in #43869 and #45235.

What if we issued a doing_it_wrong warning within _load_textdomain_just_in_time for such cases? This would urge developers to fix their code to run at the right time.

If a lot of plugins are currently doing it wrong though, that might cause a lot of noise.

Change History (2)

#1 @swissspidy
8 months ago

  • Milestone changed from Awaiting Review to Future Release

Alternatively, we could make _load_textdomain_just_in_time() do nothing if called to early (i.e. if the $current_user global is null or if ! did_action( 'init' ) && ! doing_action( 'init' )).

This would cause _load_textdomain_just_in_time() to potentially be called multiple times before init, but at least users would get the correct translations all the time, preventing issues like https://github.com/WordPress/health-check/issues/448

#2 @swissspidy
3 months ago

  • Milestone Future Release deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #44937.

Note: See TracTickets for help on using tickets.