#61206 closed enhancement (fixed)
Allow to filter the returned value of `WP_Textdomain_Registry::get()`
Reported by: | Chouby | Owned by: | swissspidy |
---|---|---|---|
Milestone: | 6.6 | Priority: | normal |
Severity: | normal | Version: | 6.1 |
Component: | I18N | Keywords: | has-patch commit |
Focuses: | Cc: |
Description
The context is locale fallback as implemented by @swissspidy in the [Preferred languages https://github.com/swissspidy/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 https://github.com/WordPress/wordpress-develop/blob/6.4.3/src/wp-includes/l10n.php#L1301-L1304].
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:
https://make.wordpress.org/core/2024/05/07/merge-proposal-preferred-languages/
https://github.com/swissspidy/preferred-languages/issues/1049
Change History (7)
#1
@
4 months ago
- Keywords has-patch added
- Milestone changed from Awaiting Review to 6.6
- Owner set to swissspidy
- Status changed from new to assigned
This ticket was mentioned in PR #6614 on WordPress/wordpress-develop by @swissspidy.
4 months ago
#2
Makes it easier for Preferred Languages and other plugins to override the determined languages folder when using just-in-time translation loading.
Trac ticket: https://core.trac.wordpress.org/ticket/61206
@swissspidy commented on PR #6614:
4 months ago
#3
cc @chouby
@swissspidy commented on PR #6614:
4 months ago
#7
Committed in https://core.trac.wordpress.org/changeset/58236
I don't know why we didn't do this earlier, it would make the Preferred Languages code base so much easier.