__() not returning original string as fallback when using PHP language file
-
I think I might have discovered a bug in how the l10n.php localization files that are supported since WP 6.5 are generated (alternatively how they are processed).
When calling __() or other translation functions on a string that does not yet have a translation, the function is supposed to return the original string. Instead, it returns an empty string.
Here is my scenario:
- I have a custom theme with a text domain. I use
wp i18n make-pot
to generate a .pot file and also use WP-CLI to update the .po files and generate .mo files. - I use
wp i18n make-php
to also generate the more performant .l10n.php files - We are in the process of adding an extra language to our website, so we have created a new .po file for that language which (at the moment) only have a few strings translated
For the untranslated strings, the generated PHP file contains entries such as:
'Log in'=>''
I’ve debugged the issue, and I can see that the problem is that the
locate_translation
function in theWP_Translation_Controller
class contains the following lines:// Find the translation in all loaded files for this text domain.
foreach ( $this->get_files( $textdomain, $locale ) as $moe ) {
$translation = $moe->translate( $singular );
if ( false !== $translation ) {
return array(
'entries' => explode( "\0", $translation ),
'source' => $moe,
);
}The code expects missing translations to be false, not an empty string. So in this case it will return the empty string as the translation.
This looks like a bug to me.
P.S.: I have already implemented a workaround for my theme. I’ve made a gettext hook that checks for such empty translations and returns the original string instead.
- I have a custom theme with a text domain. I use
- You must be logged in to reply to this topic.