The following example works on Mac OS X with Apache, i.e. I get the translated string echoed back. But on Ubuntu with lighttpd I get the original text 'Inactive account'. I've tried all sorts of combinations of environment varialbes without any luck. It's not file permissions either because I can echo out the contents of the .mo file.

<?php

//$locale = 'sv_SE.UTF-8';
$locale = 'sv_SE';

$dir = dirname(__FILE__);

// File permission is apparantly not a problem as this works...
//echo file_get_contents($dir . '/sv_SE/LC_MESSAGES/flattr.mo');

putenv("LANG=$locale");
putenv("LANGUAGE=$locale");
putenv("LC_ALL=$locale");
putenv("LC_MESSAGES=$locale");
setlocale(LC_ALL, $locale);
setlocale(LC_MESSAGES, $locale);
//setlocale(LANG, $locale);
//setlocale(LANGUAGE, $locale);

bindtextdomain('flattr', $dir);
//bind_textdomain_codeset("flattr", 'UTF-8');
textdomain('flattr');

echo _("Inactive account");

?>

Anyone have any ideas?

Comments

Has the language properly set up in the OS itself? See my previous question maybe can help stackoverflow.com/questions/1431354/php-gettext-in-norwegian

Written by fabrik

Have you generated the locale for sv_SE ? If not, then add (or uncomment) a line containing sv_SE.UTF-8 to /etc/locale.gen and then run sudo locale-gen.

sudo locale-gen sv_SE.UTF-8 (and then restart lighttpd) worked. Could you post it as an answer to this question so I can mark it as correct? Thanks!

Written by pthulin

Accepted Answer

I was facing the same problem. I'll describe the things I did to fix it on Ubuntu 10.10.

1) make sure you have 'gettext' installed,

$ sudo apt-get install gettext

Alternatively, you can install 'php-gettext' if 'gettext' cannot be installed. The package 'php-gettext' is not required if you already have 'gettext' installed.

2) Then generate the locale for your language. In this example I'll use 'sv_SE'. Look up the supported locales in '/usr/share/i18n/SUPPORTED',

less /usr/share/i18n/SUPPORTED

You'll find multiple lines that start with 'sv_SE',

sv_SE.UTF-8 UTF-8
sv_SE ISO-8859-1
sv_SE.ISO-8859-15 ISO-8859-15

This means you have multiple options for generating the locale for sv_SE. To generate the locale for the default character set, run the following command,

$ sudo locale-gen sv_SE

If you want to generate that locale for the UTF-8 character set, run this command,

$ sudo locale-gen sv_SE.UTF-8

Restart Apache after generating locales,

sudo /etc/init.d/apache2 restart

3) Finally, update your PHP script to match the locale you generated. If you generated the locale for 'sv_SE',

setlocale(LC_ALL, "sv_SE");

But if you generated the UTF-8 equivalent of that locale, use,

setlocale(LC_ALL, "sv_SE.UTF-8");

All should work now.

Written by Serrano Pereira
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki