I’m using gettext for adding different translations into my webpage. I’ve just installed it and I think it works fine. I also use Poedit for the .po files.
This is the code I have for my gettext testing:
<?php // I18N support information here $language = "en_US"; putenv("LANG=" . $language); setlocale(LC_ALL, $language); // Set the text domain as "messages" $domain = "messages"; bindtextdomain($domain, "Locale"); bind_textdomain_codeset($domain, 'UTF-8'); textdomain($domain); // // test if gettext extension is installed with php // if (!function_exists("gettext")) { echo "gettext is not installedn"; } else { echo "gettext is supportedn"; } echo '<br>'; echo _("HELLO WORLD"); echo _("TEST TRANSLATION"); ?>
That code, returns me ‘gettext is supported’, but instead of showing me the translations, it shows me ‘HELLO WORLD’ and ‘TEST TRANSLATION’.
This is my messages.po file:
msgid "" msgstr "" "Project-Id-Version: n" "POT-Creation-Date: 2014-03-22 14:45+0100n" "PO-Revision-Date: 2014-03-22 15:23+0100n" "Last-Translator: n" "Language-Team: n" "MIME-Version: 1.0n" "Content-Type: text/plain; charset=UTF-8n" "Content-Transfer-Encoding: 8bitn" "X-Poedit-KeywordsList: _;gettext;gettext_noopn" "X-Poedit-Basepath: .n" "X-Poedit-Language: Englishn" "X-Poedit-Country: UNITED STATESn" "X-Poedit-SourceCharset: utf-8n" # Test token 1 msgid "HELLO WORLD" msgstr "Hello World!" # Test token 2 msgid "TEST TRANSLATION" msgstr "Testing translation..."
And it’s inside my ‘Locale/en_US/LC_MESSAGES’ folder, with also a messages.mo file successfully converted. (I’ve hidden the info of the file, the name, the project, etc.)
What am I doing wrong?
Thanks!
EDIT: I also might add that I use Ubuntu with PHP, Apache and all this things installed.
Advertisement
Answer
I’ve been having headaches with this one too. I added this and it worked:
putenv('LANGUAGE=en_US');
Next to that I use the following:
$domain = 'woohoo'; setlocale(LC_ALL, 'en_US.utf8'); putenv('LANGUAGE=en_US'); if( ENV != 'live' ){ // reset caching nocache is a simlink to "." bindtextdomain($domain, dirname(__FILE__) . '/../locale/nocache'); } bindtextdomain($domain, dirname(__FILE__) . '/../locale'); bind_textdomain_codeset($domain, 'UTF-8'); textdomain($domain);
Also, in my setup it only started working when I added the iso code to the .po file:
msgid "" msgstr "" "Language: en_USn"