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 installed\n";
}
else
{
echo "gettext is supported\n";
}
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+0100\n"
"PO-Revision-Date: 2014-03-22 15:23+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-KeywordsList: _;gettext;gettext_noop\n"
"X-Poedit-Basepath: .\n"
"X-Poedit-Language: English\n"
"X-Poedit-Country: UNITED STATES\n"
"X-Poedit-SourceCharset: utf-8\n"
# 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.
bind_textdomain_codeset()
, the translations started working for me. Some locales can be represented by multiple charsets, and their default charset is not UTF-8 (e.g. zh_CN uses GB2312). I usedpkg-reconfigure locales
to manage the charsets. You can read this article for more information: blog.terresquall.com/2020/09/troubleshooting-php-gettext – John Doe