5
votes

I am currently trying to use gettext with PHP and poedit. I wrote the following test.php file:

<?php

error_reporting(E_ALL | E_DEPRECATED | E_USER_DEPRECATED | -1);
bindtextdomain('messages', './i18n/');
textdomain('messages');
setlocale(LC_ALL, $_GET['l']);
putenv("LANG=".$_GET['l']);

echo _('test :-(');

?>

and this is my messages.po:

msgid ""
msgstr ""
"Project-Id-Version: Community Chess\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2011-10-07 18:34+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: Martin Thom <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-SourceCharset: utf-8\n"
"X-Poedit-Basepath: /var/www/community-chess\n"
"X-Poedit-SearchPath-0: .\n"

#: test.php:8
msgid "test :-("
msgstr "Juhu :-)"

#~ msgid "test"
#~ msgstr "Juhu!"

My directory structure is

community-chess
    test.php
    i18n
        de_DE
            LC_MESSAGES
                messages.po
                messages.mo

As soon as I look at http://localhost/community-chess/test.php?l=de_DE I get "test :-("

I have generated the locale with

sudo locale-gen de_DE

and checked with

locale -a

Why doesn't it work? How can I get some feedback from gettext?

3
did you enable php_gettext in php.ini?Ariful Islam
according to phpinfo(): "GetText Support enabled"Martin Thoma
Instead of setlocale() try putenv("LANG=") and the others.mario
I tried only putenv and putenv with setlocale. Both didn't work.Martin Thoma
Gettext sucks for this exact reason. I would consider using Zend_Translate instead. It can deal with .mo/.po filesPekka

3 Answers

1
votes

This is what work for me on CE ZendServer on linux and Apache server on NetBsd

File "message.po" is generated from the root of application:

#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-04-30 19:38+0200\n"
"PO-Revision-Date: 2013-04-12 14:00+0000\n"
"Last-Translator: gin(e) <[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: eo\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: include/apteryx_text.php:3
msgid "email:"
msgstr "Retpoŝtadreso:"

This is the new tree-directory:

res/locale/
    de_DE/
       LC_MESSAGES/
            messages.mo
    eo_EO/
       LC_MESSAGES/
            messages.mo
    eo->eo_EO (symlink)

I must add the "eo" symlink because the only three esperanto locales on my system supported are:

eo
eo.iso88593
eo.utf8

compiled whit:

sudo locale-gen eo
sudo locale-gen eo.iso88593
sudo locale-gen eo.utf8
sudo update-locale
sudo dpkg-reconfigure locales

and at the moment my locale.php code is:

$charset="UTF-8";
$gettext_domain="messages";
$locale_dir="res/locale";
putenv("LC_ALL=$lang");
$l=split("_",$lang);
/* not in all system and not all locales got the classic name this stupid method try to solve it*/
if(! setlocale(LC_ALL, $lang.".".$charset)) 
  if(! setlocale(LC_ALL, $lang)) 
    if(! setlocale(LC_ALL,$l[0].".".$charset))  
        setlocale(LC_ALL,$l[0]);

bindtextdomain($gettext_domain, "res/locale");
textdomain($gettext_domain);
bind_textdomain_codeset($gettext_domain, $charset);

because whitout it gettext doesn't work. I think that the locale directory must have the same name of the language setted whit setlocale. Another things is to test every function called what return. Them must never return NULL or FALSE. You can do that in simple way:

var_dump(bindtextdomain($gettext_domain, "res/locale"));
var_dump(textdomain($gettext_domain)); 
..and so on..

The last but not the list, remember to set the right apache permission to all .mo file, to restart your apache server and verify by phpinfo() that "GetText Support" is "enabled".

1
votes

When I restart nginx through the cmd "/path/to/nginx -s reload",it dosen't work,but after I restart php-fpm with the cmd "/etc/init.d/php-fpm restart",it worked! Hope my experience will be helpful to someone with the issue :)

0
votes

In case you missed it, the OP said in his comments above that he fixed this by simply restarting Apache. I was having troubles with _() just not working. The locale was setting fine, bindtextdomain and textdomain were returning the correct values, but it just wasn't working. I restarted Apache and it worked.