Question

Running php-5.3.10

This is my first time setting up a website with i18n via gettext. However, the English text is still being printed, instead of the Dutch text I'm expecting. I've been Googling for two days now, but I still haven't found a solution.

The php code, even when run from cli it does not give a translation, so Apache caching is not the issue here. The code:

<?php
// no errors are thrown, sadly
error_reporting(E_ALL);

$domain = 'test';

setlocale(LC_ALL, 'nl_NL.utf8'); // returns 'nl_NL.utf8', I've also tried without '.utf8' etc.
bindtextdomain($domain, dirname(__FILE__) '/locale'); // returns correct path
bind_textdomain_codeset($domain, 'UTF-8');
textdomain($domain); // returns 'test'

// this prints the date in Dutch so setlocale works
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 12, 22, 1978));

// this should print the translated version but just prints 'Home'
echo ' ' . gettext('Home');

The Dutch locale is installed according to 'locales -a' and is listed as:

nl_AW
nl_AW.utf8
nl_BE.utf8
nl_NL.utf8

This is the .po file:

msgid ""
msgstr ""
"Language: nl\n"
"Content-Transfer-Encoding: 8bit\n"
"Content-Type: text/plain; charset=UTF-8\n"

msgid "Home"
msgstr "Thuis"

And it's correctly compiled to a .mo file, msgunfmt gives no errors at least. I've tried to put this file into locale/nl/LC_MESSAGES/, locale/nl_NL/LC_MESSAGES/ and locale/nl_NL.utf8/LC_MESSAGES/. To no avail.

I'm mainly looking for a way to debug this. This whole trial and error approach is getting a bit time consuming.

Was it helpful?

Solution

I found the solution, but no clue WHY I had to do this. For both cli and apache I managed to get translations working. This is what I did.

putenv('LANGUAGE=nl_NL');

Using LANGUAGE together with LC_ALL in putenv also breaks the translation for Apache, so do not use:

putenv('LC_ALL=nl_NL');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top