__() not returning original string as fallback when using PHP language file

[ad_1]

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:

  1. 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.
  2. I use wp i18n make-php to also generate the more performant .l10n.php files
  3. 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 the WP_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.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer