Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get gettext working in PHP, Linux

Tags:

php

gettext

I've been struggling with Gettext now for ages and just can't get it to work. I would be forever grateful if someone could help me with this... I have looked for solutions and tried several variants but nothing is working for me.

I have created the following file structure:

/var/www/battlestox/battlestox.com/sites/battlestox/www/locale/de_DE/LC_MESSAGE/messages.po /var/www/battlestox/battlestox.com/sites/battlestox/www/locale/de_DE/LC_MESSAGE/messages.mo

the data in the .po file is simply:

msgid ""

msgstr ""

"Content-Type: text/plain; charset=utf-8\n"

"Content-Transfer-Encoding: 8bit\n"

msgid "qwerty"

msgstr "The Translator works!"

The php file hase the following code:

putenv("LANG=de_DE.UTF-8");
setlocale(LC_MESSAGES, 'de_DE.UTF-8');
bindtextdomain('messages', 'locale');
bind_textdomain_codeset('messages', 'UTF-8');
textdomain('messages');

echo gettext("qwerty");
echo _("qwerty");

output: qwertyqwerty - so no translation happening. when I echo bindtextdomain('messages', 'locale'); I get:

/var/www/battlestox/battlestox.com/sites/battlestox/www/locale

so I believe it's pointing to the correct location..

I have checked that gettext is installed in php and locales on the server are available..

:locale -a

de_DE

de_DE.iso88591

de_DE.iso885915@euro

de_DE.utf8

de_DE@euro

I'm running PHP 5.2, Apache 2.2.3, CentOS

Please be gentle, I am very basic on linux..

Thanks for any help!

like image 251
IanM Avatar asked May 10 '26 12:05

IanM


1 Answers

Try using the exact locale name reported by locale -a. e.g. setlocale(LC_MESSAGES, 'de_DE.utf8');

like image 197
John Watson Avatar answered May 13 '26 02:05

John Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!