Manu
Manu

Reputation: 4500

Nothing gets translated using gettext

How can I debug my use of gettext ? I have followed the steps described here, created both .pot .po files, and generated a .mo file.

Yet when I launch my page, nothing is translated, and there is no error message, so I don't know what's wrong.

Here's how I've created the translation files (the project name is 'ft') :

find ft/. -name "*.php" ! -path "*.svn*" > ft/i18n/listpot.txt
xgettext --from-code=utf-8 --default-domain=ft --output=ft/i18n/ft.pot --files-from=ft/i18n/listpot.txt
rm ft/i18n/listpot.txt
#wrote the translated strings in poedit
msgfmt -c -v -o ft/i18n/en/LC_MESSAGES/en.mo ft/i18n/ft_en.po
#restarted apache

In my ft.php page I include i18n.php which contains :

    $folder     = 'ft';
    $lang       = 'en_US.UTF8';
    $domain     = 'messages';

setlocale(LC_ALL, 'en_US.UTF8', 'en.UTF8', 'en_US.UTF-8', 'en.UTF-8')."<br />";
putenv("LC_ALL=$lang");

bindtextdomain("$domain", dirname(__FILE__)."/$folder/i18n");
textdomain("$domain");
bind_textdomain_codeset("$domain", 'UTF-8');

In ft.php I echo strings with :

echo _('test_string');

But this echoes 'test_string', not the translated version :(

Is there a way to display errors generated by the php-gettext script ? Am I forgetting a step ?

edit : I have the following folder tree :

./ft/i18n/en/LC_MESSAGES/messages.mo 
                         messages.po 
          fr/LC_MESSAGES/messages.po 
                         messages.mo 

I have tried changing the "en" folder to "en_US" and "en_US.UTF8", and the "messages.mo" file to "en.mo", "en_US.mo", "en_US.UTF8.mo", but nothing gets translated.

Upvotes: 1

Views: 2540

Answers (1)

Manu
Manu

Reputation: 4500

gettext is not very easy to debug, as it doesn't say anything when something is wrong. Try using is_dir() to check the folder where the .po and .mo files are.

I also had a problem with variable scope in a require()

Here's what works for me :

$folder     = 'ft';
$domain     = 'messages';
$lang       = 'fr_FR.utf8';

$directory = dirname(__FILE__)."/../../$folder/i18n";

//putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows
setlocale(LC_MESSAGES, $lang);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

The directories are :

./ft/i18n/en/LC_MESSAGES/messages.mo 
                         messages.po 
          fr/LC_MESSAGES/messages.po 
                         messages.mo

Upvotes: 4

Related Questions