1
votes

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.

4
@IliyaReyzis Nope, I'm using Ubuntu with PHP, Apache, Mysql.. Installed in it.peregraum
After I made sure that the generated locale file shares the same charset as my MO file and 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 use dpkg-reconfigure locales to manage the charsets. You can read this article for more information: blog.terresquall.com/2020/09/troubleshooting-php-gettextJohn Doe

4 Answers

6
votes

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_US\n"
1
votes

For me, the solution was to use "en" for the directory name, rather than "en-US".

PHP 5.2, gettext 0.19.8.1, Ubuntu 18.04, apache2

0
votes

Two things i would consider:

  • check (e.g. by using locale -a) if the language you want to use is installed a more detailed description can be found here: here

  • check if apache is caching your file, either "disable" it or restart apache to get the new file. more info on previous link and here

0
votes

just add following line

putenv("LANGUAGE=en_US");