Make WordPress Core

Opened 5 years ago

Closed 2 months ago

#47547 closed defect (bug) (invalid)

switch_to_locale( 'en_US' ) is unreliable

Reported by: johnbillion's profile johnbillion Owned by: johnbillion's profile johnbillion
Milestone: Priority: normal
Severity: normal Version:
Component: I18N Keywords: needs-patch reporter-feedback close
Focuses: administration Cc:

Description (last modified by garrett-eclipse)

Given a site that uses en_US as its language, a user with their user language set to something else, and that user logged into the admin dashboard, calling switch_to_locale( 'en_US' ) appears to be unreliable.

  1. ❌The return value of determine_locale() is the user's language, not en_US.
  2. ✅Localised text without a textdomain (ie. text from core) is correctly displayed in en_US.
  3. ❌Localised text with a textdomain (ie. text from plugins or themes) is incorrectly displayed in the user's language. This may be related to #39210.

Given a site that itself uses a language other than en_US, calling switch_to_locale( 'en_US' ) does appear to work although I haven't tested it extensively.

Change History (8)

#1 @garrett-eclipse
5 years ago

  • Description modified (diff)

I may be off-base but I believe some findings here can shed some light;
"switch_to_locale() only does something if the locale passed to it is different than the value of get_locale() outside of the admin. Inside the admin, it only switches if it's different than the locale of the current user."

#2 @garrett-eclipse
5 years ago

  • Description modified (diff)

Sorry about the emoji's disappearing, was fixed in meta#3838 so added them back.

#3 @swissspidy
11 months ago

  • Keywords reporter-feedback added

Is this still an issue?

#4 @johnbillion
11 months ago

I believe it is but I'll do some testing to confirm

#5 @swissspidy
3 months ago

  • Keywords close added

#6 @johnbillion
3 months ago

  • Owner set to johnbillion
  • Status changed from new to accepted

#7 @swissspidy
3 months ago

FWIW in my Preferred Languages plugin I have some e2e tests for the dashboard where I switch to en_US and strings are then in en_US as expected and determine_locale() returns en_US too. But might just be because of the plugin :)

#8 @johnbillion
2 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from accepted to closed

I can't reproduce this any more but I'm not 100% confident in my tests. I'll close this off, it can always be reopened if the issue remains.

Note: See TracTickets for help on using tickets.