PHP: Gettext problems
I can't seem to get gettext working, here.
I'm using MAMP Pro and phpinfo()开发者_开发问答;
says that gettext is enabled.
I followed this tutorial: http://zez.org/article/articleview/42/1/
- I have my strings declared in my code like this:
_('test string');
- Then I ran
xgettest -L PHP test.php
, which generated mymessages.po
file. - I opened this file in poedit and translated my strings, then saved the file, which made a
messages.mo
file. - I took both the
.po
and.mo
files and moved them to[webroot]/locale/es/LC_MESSAGES/
(in this case, my translation was Spanish) In my code, I set the following:
$lang = $_SESSION['lang']; // available to all views that include this file
putenv('LC_ALL='.$lang);
bindtextdomain('messages','./locale');
textdomain('messages');
... where
$_SESSION['lang']
I can easily toggle betweenen
andes
.I reload my page, then click on my link that toggles to
es
, but my strings are still showing up as the English versions and not the Spanish versions. Toggling back toen
shows no change; it's always the English versions.
I get no error message anywhere so I'm a bit lost as to why I'm having this problem. Can anyone give me some guidance/help? Did I miss something?
It seems as though the problem may be with poedit. Originally, all my Spanish translations I had marked as "fuzzy". If I remove "fuzzy" from them, they show up on my webpage like they should.
I don't think this is normal behavoir, but hey, at least it works.
You also need to install the corresponding language files on your server as so:
cat /usr/share/i18n/SUPPORTED |grep -i "es_ES" > /var/lib/locales/supported.d/es
cat /usr/share/i18n/SUPPORTED |grep -i "nl_NL" > /var/lib/locales/supported.d/nl
cat /usr/share/i18n/SUPPORTED |grep -i "de_DE" > /var/lib/locales/supported.d/de
Now you installed the languages Spanish, Dutch and German. When finished, run dpkg-reconfigure locales and you are done. =)
精彩评论