Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gettext not working through php-cli, but works in php-apache

The code i run looks as the following:

...
$this->locale = da_DK;
...
putenv("LC_ALL=".$this->locale);
putenv('LANG='.$this->locale);
$res = setlocale(LC_ALL, $this->locale);
if($res != $this->locale){
    throw new Exception("The language could not be set.");
}
bindtextdomain("domain", "./locale");
textdomain("domain");

 echo setlocale(LC_MESSAGES, 0 );
 echo ' ';
 echo __('description');

Result running it through apache2 is:

da_DK Beskrivelse

Which is as we want.

Running it throgh cli:

da_DK Description

Which would have been correct if we had used english. The 'd' is upper case in the english translation and the lower case in the source (From out of context it seems a bit weird)

So gettext works as it can translate the string, but somehow it disregards that i changed locale in the script and chose the .po file from the english directory.

In case you wonder why i need locales on a cli script: The script is used to emails out invoices.

I use: Ubuntu 12.10, PHP 5.4.6-1ubuntu1, apache 2.2.22, and gettext 0.18.1.

like image 908
CodeTower Avatar asked Nov 04 '12 16:11

CodeTower


1 Answers

You need to check if the bindtextdomain() points the same resource file from php-cli and php-apache. I suggest,

  1. echo the return value of the bindtextdomain() and see them.
  2. try to replace "./locale" to absolute path (e.g. "/var/www/yourproject/locale") and see if it works.
like image 129
akky Avatar answered Sep 27 '22 18:09

akky