Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translating empty string #182

Closed
dmvdbrugge opened this issue May 18, 2018 · 4 comments
Closed

Translating empty string #182

dmvdbrugge opened this issue May 18, 2018 · 4 comments

Comments

@dmvdbrugge
Copy link

In po and mo files (I don't know about others), the message with empty string as id contains the file headers. These should never be returned as a translation. Is it an idea to either

a. leave them out during the reading of the file, or
b. just return empty string when translation of key empty string is requested?

I'm willing to make a PR myself if you agree with either of the options, or interested in possible other solutions.

@oscarotero
Copy link
Member

In theory, these messages are parsed as headers. Do you have an example of po file that is not parsed correctly?

@dmvdbrugge
Copy link
Author

So a little background first: we were using the xgettext, msgmerge, and msgfmt utils, in combination with Zend_Translate, but since they don't support both context and domain we switched to Gettext. We thus already had the entire "generate .mo file" flow in place, and use Gettext only for the fromMoFile to translations part.

I've attached a sample .po and resulting .mo file. Loading that .mo file and requesting it translates empty string.

In an interactive shell:

php > $translations = Gettext\Translations::fromMoFile(__DIR__ . '<resourcedir>/checkout.nl.mo');
php > $translations->setLanguage('nl');
php > $translations->setDomain('checkout');
php > $t = new Gettext\Translator();
php > $t->loadTranslations($translations);
php > echo $t->dpgettext('checkout', '', '');
Project-Id-Version: <redacted-id>
Report-Msgid-Bugs-To:
Last-Translator: <redacted-name> <redacted-email>, 2018
Language-Team: Dutch (<redacted-url>)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
POT-Creation-Date: 2018-05-18 08:45+0000
PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
Language: nl
Plural-Forms: nplurals=2; plural=n != 1;
X-Domain: checkout

po-mo.zip

We can of course prevent this in our own wrapper, but I thought it would be nice as Gettext itself prevented leaking headers in the first place (as apparently Zend_Translate did as well).

@oscarotero
Copy link
Member

Ok, I see.
Gettext\Translator use internally an array with all translations, so if you load the translations using a Gettext\Translations, it's converted internally to an array before use it, using the Gettext\Generator\PhpArray generator, that by default includes the headers in the translations.
A workaround to this problem could be generate the array by yourself, using this code:

$filename = 'translations/checkout.nl.php';
$translations->toPhpArrayFile($filename, ['includeHeaders' => false]);
$t = new Gettext\Translator();
$t->loadTranslations($filename);

The option includeHeaders can be passed to do not include the headers in the array. This method also greatly improved the performance because the translator does not have to do anything, the array is ready to use.

Maybe the translator could include the 'includeHeaders = false' option to generate the array internally, instead use the default config. I'll change it in the new version.

@oscarotero
Copy link
Member

v4.6.0 released including a fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants