[wp-trac] [WordPress Trac] #61206: Allow to filter the returned value of `WP_Textdomain_Registry::get()`
WordPress Trac
noreply at wordpress.org
Mon May 13 12:01:12 UTC 2024
#61206: Allow to filter the returned value of `WP_Textdomain_Registry::get()`
-------------------------+-----------------------------
Reporter: Chouby | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: I18N | Version: 6.1
Severity: normal | Keywords:
Focuses: |
-------------------------+-----------------------------
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
--
Ticket URL: <https://core.trac.wordpress.org/ticket/61206>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list