Zend translate() function problem
-
10-10-2019 - |
Question
I'm using Zend Framework for a miltilingual site. Translating is done with Zend_Translate_Adapter_Gettext. I'm preparing the .mo files using poedit.
The problem is when I set up two msgids and one has a msgstr identical to the other msgid:
#: application/modules/foobar/views/scripts/index.phtml:1
msgid "foo"
msgstr "bar"
#: application/modules/foobar/views/scripts/index.phtml:2
msgid "bar"
msgstr "baz"
When I use this code:
<?php echo $this->translate('foo'); ?>
<?php echo $this->translate('bar'); ?>
the output is this:
bazbaz
I figure that if the msgstr coincides with a different msgid, it is used as if it was a msgid and thus is translated again. Please correct me if my reasoning is wrong.
Now, I'd like to know whether you've had a similar problem and how to circumvent it painlessly.
My current solution includes changing the msgids:
#: application/modules/foobar/views/scripts/index.phtml:1
msgid "KEY_FOO"
msgstr "bar"
#: application/modules/foobar/views/scripts/index.phtml:2
msgid "KEY_BAR"
msgstr "baz"
and then:
<?php echo $this->translate('KEY_FOO'); ?>
<?php echo $this->translate('KEY_BAZ'); ?>
This is not a satisfactory solution since I'm working on a community portal with ~10k files and I can't really check all of it for conflicts.
If it's of any use:
- Zend Framework version: 1.10.8
- poedit version: 1.4.6
- site runs on Apache 2.2.11 with PHP 5.3
[EDIT]
Thanks to Gordon, I can include another piece of data: a test involving PHP's gettext
. I used the very same test .mo file, including "foo"->"bar" and "bar"->"baz" key-value pairs. The PHP code was this:
<?php
putenv('LC_ALL=pl_PL');
setlocale(LC_ALL, 'pl_PL');
bindtextdomain("pl", ".");
textdomain("pl");
echo gettext("foo");
echo gettext("bar");
?>
And the result:
barbaz
So it's definitely not gettext
's fault.
Solution
change the order
#: application/modules/foobar/views/scripts/index.phtml:2
msgid "bar"
msgstr "baz"
#: application/modules/foobar/views/scripts/index.phtml:1
msgid "foo"
msgstr "bar"