0

I've been having problems getting the translations to work on my local WAMP (v2.5) server. Here is an example of the issue.

putenv("LANG=frc");                          //output: 1
setlocale(LC_ALL, "frc");                    //output: French_Canada.1252

$domain = 'messages';

bindtextdomain($domain, "locale");           //output: D:\wamp\www\[project folder]\locale
bind_textdomain_codeset($domain, 'UTF-8');   //output: UTF-8
textdomain($domain);                         //output: messages

echo gettext("Hello");                       //output: Hello

And here is the folder structure:

locale
     --> French_Canada.1252
        --> LC_MESSAGES
            --> messages.po
            --> messages.mo

And finally the PO file:

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2016-02-10 14:01-0500\n"
"PO-Revision-Date: 2016-02-10 14:01-0500\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr_CA\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.8.2\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Poedit-SearchPath-0: .\n"

#: test.php:11
msgid "Hello"
msgstr "Bonjour"

Here's what I've tried:
1) Uninstalling 64 bit WAMP and installing 32 bit WAMP in its place.
2) Using different locale codes like fr_CA, fr_FR, fr. fr is the only one that is recognized but I still had the same issue (even after renaming the folder to fr)
3) Changing the codeset to 1252 and Windows-1252 (In both the php file and the PO file)
4) Restarting all services after every change

Falantar
  • 73
  • 9
  • FYI, gettext stopped working properly on Windows after PHP/5.3 (see [Gettext will always use system default locale](http://stackoverflow.com/questions/19569362/gettext-will-always-use-system-default-locale)). Whatever, I recommend you rename your folder from `French_Canada.1252` to something like `fr`. – Álvaro González Feb 10 '16 at 19:15
  • I had no choice but to use [php-gettext](https://launchpad.net/php-gettext/) – Falantar Feb 10 '16 at 20:26

1 Answers1

2

Use GetText Class : https://github.com/oscarotero/Gettext And load your .po file differently on local or on the web.

use Gettext\Translator;
use Gettext\GettextTranslator;

$locale = !empty($_GET['locale']) ? $_GET['locale'] : Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); 
$_SESSION['locale'] = $locale;
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
$domain = 'traduction';

if(strstr($_SERVER['HTTP_HOST'], '127.0.0.1')) {

    $t = new Translator();

    //Create a Translations instance using a po file
    $translations = Gettext\Translations::fromPoFile('locale/'.$locale.'/LC_MESSAGES/'.$domain.'.po');
    $t->loadTranslations($translations);


} else {

    //Create the translator instance
    $t = new GettextTranslator();

    //Set the language and load the domain
    $t->setLanguage($locale);
    $t->loadDomain($domain, 'Locale');

}

//If you want use the global functions
$t->register();

echo __('Pomme'); // "Apple"
echo n__('%d fenetre', '%d fenetres', 2, 2); // "2 windows"