Translations don’t waterfall
-
For this experiment I took the Twenty Twenty-Five theme and added to its’
functions.php
:add_action('init', function() {
load_theme_textdomain('twentytwentyfive', __DIR__ . '/');
});
add_shortcode('test', function() {
$content = '<ul>';
$content .= sprintf('<li>String 1: %s</li>', esc_html__('String 1', 'twentytwentyfive'));
$content .= sprintf('<li>String 2: %s</li>', esc_html__('String 2', 'twentytwentyfive'));
$content .= '</ul>';
return $content;
});Then I used Loco to create the
.pot
as well as a Language that translatesString 1
. I chose the “Author” locationthemes/twentytwentyfive/{locale}.po
so as to simulate translations bundled with a non-wp.org theme.Loco created:
twentytwentyfive.pot
,{locale}.l10n.php
,{locale}.mo
and{locale}.po
, the last 3 I renamed totwentytwentyfive-{locale}.l10n.php
,twentytwentyfive-{locale}.mo
andtwentytwentyfive-{locale}.po
because it didn’t work without it.Now on a page where I use the
test
shortcode theString 1
is translated andString 2
is not, as expected.Then as a “user of the theme” I use Loco to add a translation to the
String 2
– I go to Loco -> Themes -> Twenty Twenty-Five -> New language and create it at the “Other” locationlanguages/loco/themes/twentytwentyfive-{locale}.po
, I translateString 2
and save.Now the
String 2
is translated, butString 1
isn’t – the translations didn’t waterfall.—
Seems like when a custom Loco translation is added, it loads that one and disregards the original “author” one.
A manual fix:
add_filter('load_translation_file', function($file, $domain, $locale) {
static $fixed = false;
if($fixed) {
return $file;
}
if($domain === 'twentytwentyfive' && $locale === '{my_locale}') {
$file = wp_normalize_path($file);
if(strpos($file, 'languages/loco/themes') !== false) {
$fixed = true;
add_action('init', function() use ($file) {
$author_file = str_replace('languages/loco/themes', 'themes/twentytwentyfive', $file);
$author_file = str_replace('.l10n.php', '.mo', $file);
load_textdomain('twentytwentyfive', $author_file);
}, 15);
return str_replace('languages/loco/themes', 'themes/twentytwentyfive', $file);
}
}
return $file;
}, 15, 3);So when Loco goes to load
languages/loco/themes/twentytwentyfive-{locale}.l10n.php
I intercept and change it to the author path instead, and also need to doload_textdomain
again with the.mo
file, for some reason. Then the author file gets loaded correctly and Loco ends up loading the custom file again at some point, resulting in both strings translated.—
Can you reproduce it? Am I doing something wrong?
- You must be logged in to reply to this topic.