Pat
Pat

Reputation: 509

Using Gettext for multilingual site in php doesn't work

I'm trying to make a multilingual site in PHP using gettext.

I'm using PHP7.1 (no way of upgrading), and have enabled gettext.so in my php.ini.

See: http://corbeauperdu.ddns.net/phpinfo.php

I've read the documentation and followed examples, but for some reason, it won't translate at all to my French language.

My PO file and test.php files are as follows:

/prestadesk/include/locales/fr/LC_MESSAGES/prestadesk.po:
msgid ""
msgstr ""
"Language: fr\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

msgid "This page will show the dashboard"
msgstr "Cette page affichera le tableau de bord"


/prestadesk/templates/test.php:
<?php
$lang='fr';
$domain = 'prestadesk';
$codeset = 'UTF-8';
$locales_dir = '../include/locales'; // need to go up on directory from here to get into the include/locales

// here we define the global system locale given the found language
putenv('LANG='.$lang);

// this might be useful for date functions (LC_TIME) or money formatting (LC_MONETARY), for instance
setlocale(LC_ALL, $lang);

// this will make Gettext look for $locales_dir/<lang>/LC_MESSAGES/prestadesk.mo
bindtextdomain($domain, $locales_dir);

// indicates in what encoding the file should be read
bind_textdomain_codeset($domain, $codeset);

// here we indicate the default domain the gettext() calls will respond to
textdomain($domain);

// test translate
echo gettext("This page will show the dashboard");
?>

Upvotes: 0

Views: 111

Answers (1)

Pat
Pat

Reputation: 509

Well, I found the problem:

The server the website resides on NEEDS to have the locale installed for the desired $lang !!!

On the server, check the installed locales with:

locale -a

If your desired locale is not listed, you must edit /etc/locale.gen and uncomment yours. Example:

fr_FR.UTF-8 UTF-8 
fr_FR ISO-8859-1
fr_FR@euro ISO-8859-15

Then, generate and install that locale:

sudo locale-gen

Once this is done, you'll be able to use gettext() properly.

Upvotes: 1

Related Questions