Question

I'm having problems with gettext in PHP; i have two servers, one local (in which I development) in Windows (with vs.php) and works find, and other in Amazon EC2 (Ubuntu 12.04 updated), this is the production server and in this server the same code don't found.

This is my code:

I initialice the gettext with this.

function initialize_i18n($locale) {
    $locales_root = "./librerias/noAuto/locale";
    putenv('LANG='.$locale);
    putenv("LC_ALL=" . $locale);
    setlocale(LC_ALL,$locale);
    $domains = glob($locales_root.'/'.$locale.'/LC_MESSAGES/messages*.mo');
    if (count ($domains) > 0)
    {
        $current = basename($domains[0],'.mo');
        $timestamp = preg_replace('{messages-}i','',$current);
        bindtextdomain($current,$locales_root);
        bind_textdomain_codeset( $current, "UTF-8" );
        textdomain($current);
        if ($locale == "en_US")
        {
            if( _("Modificar") === "Modify" ) {
                $system->setDebug( "Translated correctly");
            } else {
                $system->setDebug( "Gettext don't working");
            }
        }
    }
}
initialize_i18n("en_US");

All files in my projects is coded in UTF-8 (in spanish Language), and the .mo and .po has been generated with poedit.

I try to reload apache service but did not work.

Any idea?

Was it helpful?

Solution

New day new ideas! I have a solution!

I you have the same problem, you need checks this steps.

1 - Your Linux have this language installed?

You can check with this:

 locale -a

If don't listed your language install it, for example you can install englsh localization with this:

 # aptitude install language-pack-en

And restart apache when finish.

2 - Your language code is correctly in PHP?

If you listed the list of language codes with locale -a you will see the language codes that you can use, for example, for english my system listed: [...] en_NG.utf8 en_NZ.utf8 en_PH.utf8 en_SG.utf8 en_US.utf8 [...] One my error are, I used "en_US" instead of "en_US.utf8".

3 - The folder have the SAME name as the language code. (My second error)

PD> Sorry for my bad english

OTHER TIPS

If it does not work in different environment, you should better check if the key functions are successfully processed or not.

I suggest you to check the return values of bindtextdomain() and bind_textdomain_codeset() first. If they return blank, it means that PHP can not find the message resource file (only on Ubuntu) then you may check permissions of folders and files from an Unix user who runs your web server and PHP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top