Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#57116 closed defect (bug) (fixed)

Plugin translations not loaded after calling `switch_to_locale()`

Reported by: johnbillion Owned by: swissspidy
Priority: normal Milestone: 6.2
Component: I18N Version: 6.1
Severity: normal Keywords: has-patch has-unit-tests commit fixed-major
Cc: Focuses:

Description

I'm still trying to narrow down the issue but it seems that [54682] in 6.1 caused an issue where plugin translation strings are not loaded after calling switch_to_locale(). WordPress core translations are correctly loaded in the same situation.

Not yet sure if this is the same issue as #57084.

Minimal code to reproduce

Assuming a site that uses en_US, a translation file for it_IT is present, and the relevant translation files in place:

<?php
$switched_locale = switch_to_locale( 'it_IT' );
_e( 'My text string', 'my-textdomain' );
var_dump( $switched_locale );

The locale is correctly switched but the text string is output in the original language instead of the target language.

Testing with the User Switching plugin

My User Switching plugin includes this functionality and includes acceptance tests that started failing with WordPress 6.1. See ​https://github.com/johnbillion/user-switching/actions/runs/3470688222/jobs/5799202419 for a failing CI run.

  1. Install ​the User Switching plugin
  2. Install the Italian language pack for core
  3. Visit the Updates screen and update translations for plugins
  4. Add an Administrator user with their language set to it_IT
  5. Add an Author user with their language set to en_US
  6. Log in as the admin user
  7. Switch to the Author user
  8. Observe the confirmation message is shown in English instead of Italian

Change History (12)

#1 @swissspidy
4 years ago

Haven't checked yet, but this might have to do with using load_plugin_textdomain() but not having any actual translation files in the languages folder:

​https://github.com/johnbillion/user-switching/blob/ccbc248a1e5f9db44e207f7113c96dbe409c14ac/user-switching.php#L168

Does the switching work once you remove that call?

#2 @johnbillion
4 years ago

Nope, no change when removing that call.

This ticket was mentioned in ​PR #3640 on ​WordPress/wordpress-develop by ​@swissspidy.


4 years ago
#3

  • Keywords has-unit-tests added

Try to be less smart with the cache in the has() method and actually do what it’s supposed to do:

Determine whether any MO file paths are available for the domain, regardless of locale.

To-do

  • [ ] Write better tests that better capture the bug from the minimal code to reproduce

Trac ticket: https://core.trac.wordpress.org/ticket/57116

This ticket was mentioned in ​PR #3640 on ​WordPress/wordpress-develop by ​@swissspidy.


4 years ago
#4

Try to be less smart with the cache in the has() method and actually do what it’s supposed to do:

Determine whether any MO file paths are available for the domain, regardless of locale.

To-do

  • [ ] Write better tests that better capture the bug from the minimal code to reproduce

Trac ticket: https://core.trac.wordpress.org/ticket/57116

#5 @swissspidy
4 years ago

  • Milestone Awaiting Review → 6.1.2

I was able to reproduce this and saw how the cache wasn't working as intended for this use case. There's a draft PR to fix this, but I have yet to write better tests that actually catch this, as none of the existing ones have done so unfortunately.

​@johnbillion commented on ​PR #3640:


4 years ago
#6

Tested this locally and it fixes the issue in the User Switching plugin 👍

#7 @swissspidy
4 years ago

  • Keywords commit added
  • Owner set to swissspidy
  • Status new → assigned

#8 @swissspidy
4 years ago

  • Resolution → fixed
  • Status assigned → closed

In 55010:

I18N: Change how WP_Textdomain_Registry caches translation information.

WP_Textdomain_Registry was introduced in [53874] and later adjusted in [54682] to store text domains and their language directory paths, addressing issues with just-in-time loading of textdomains when using locale switching and load_*_textdomain() functions.

This change improves how the class stores information about all existing MO files on the site, addressing an issue where translations are not loaded after calling switch_to_locale().

Props johnbillion, ocean90, SergeyBiryukov.
Fixes #57116.

#10 @swissspidy
4 years ago

  • Keywords fixed-major added
  • Resolution fixed
  • Status closed → reopened

#11 @SergeyBiryukov
4 years ago

  • Milestone 6.1.2 → 6.2.1

Moving to 6.2.1, as there are no plans for 6.1.2 at this time.

#12 @SergeyBiryukov
4 years ago

  • Milestone 6.2.1 → 6.2
  • Resolution → fixed
  • Status reopened → closed

This is already included in the 6.2 branch as of [55504], no backport needed.

Note: See TracTickets for help on using tickets.