Make WordPress Core

Opened 5 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#61206 closed enhancement (fixed)

Allow to filter the returned value of `WP_Textdomain_Registry::get()`

Reported by: chouby's profile Chouby Owned by: swissspidy's profile swissspidy
Milestone: 6.6 Priority: normal
Severity: normal Version: 6.1
Component: I18N Keywords: has-patch commit
Focuses: Cc:


The context is locale fallback as implemented by @swissspidy in the [Preferred languages] plugin or a similar feature in Polylang Pro.

When a translation file in the current locale is not found for a plugin or theme, the Preferred languages plugin allows to fallback to a different locale.
This is especially useful when a language variant has less translations available than another. For example fr_BE has less language packs available than fr_FR.

The feature is easy to implement with existing WP filters for plugins or themes loading explicitely their translation using load_plugin_texdomain() or load_theme_texdomain().

This is trickier when the translations are loaded by _load_textdomain_just_in_time(). The reason can be found in the implementation of the function. If no translation file is found by WP_Textdomain_Registry, then [the function simply bails early].
There is currently no filter in WP_Textdomain_Registry or _load_textdomain_just_in_time() which could allow to provide an alternative translation file.

The Preferred languages plugin works around the limitation by using the gettext filter family. This means that the code to implement the locale fallback is typically executed thousands of times (once per call to the __() function family) when we would ideally want to execute it only once per text domain.

Adding a filter to modify the returned value of WP_Textdomain_Registry::get() would allow to implement locale fallbacks more efficiently.

Related: #28197

For references:

Change History (7)

#1 @swissspidy
3 weeks ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 6.6
  • Owner set to swissspidy
  • Status changed from new to assigned

I don't know why we didn't do this earlier, it would make the Preferred Languages code base so much easier.

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

3 weeks ago

Makes it easier for Preferred Languages and other plugins to override the determined languages folder when using just-in-time translation loading.

Trac ticket:

@swissspidy commented on PR #6614:

3 weeks ago

cc @chouby

@Chouby commented on PR #6614:

3 weeks ago

It's perfect for me :)

#5 @johnbillion
3 weeks ago

  • Keywords commit added

#6 @swissspidy
3 weeks ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 58236:

I18N: Add new lang_dir_for_domain filter.

This new filter in WP_Textdomain_Registry allows plugins to override the determined languages folder when using just-in-time translation loading.

Props swissspidy, chouby, johnbillion.
Fixes #61206.

Note: See TracTickets for help on using tickets.