I read a lot of tutorials and articles about localization and PHP's gettext()
, as well as a bunch of questions on Stack Overflow, but I can't figure this out. It seems I'm doing everything right, but nothing works.
I want to turn hello
into HALLO
with German (de_DE) localization.
PHP version: 7.1.6
Server: Apache
Folder structure:
localize
/locale
| /de_DE
| /LC_MESSAGES
| messages.mo
| messages.po
index.php
messages.po:
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2017-09-21 15:30+0300\n"
"PO-Revision-Date: 2017-09-21 15:30+0300\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.3\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Poedit-SearchPath-0: .\n"
#: index.php:15
msgid "hello"
msgstr "HALLO"
index.php:
<?php
$locale = 'de_DE';
$domain = 'messages';
putenv("LC_ALL=$locale");
echo 'LC_ALL: ' . LC_ALL . '<br>';
$loc_de = setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge');
echo "Preferred locale for German is '$loc_de'<br>";
echo 'Text domain bound: ' . bindtextdomain($domain, "./locale") . '<br>';
echo 'Text domain set: ' . textdomain($domain) . '<br>';
echo '<h1>' . gettext('hello') . '</h1>';
index.php output:
LC_ALL: 0
Preferred locale for German is 'de'
Text domain bound: C:\xampp\htdocs\localize\locale
Text domain set: messages
hello
What I've tried
- I compiled my messages.po into messages.mo multiple times with Poedit.
- I restarted my Apache server multiple times.
- After seeing
de
is the preferred locale, I tried setting the whole$locale
tode
. - I tried using
LANG
instead ofLC_ALL
.
None of that made a difference.
What am I doing wrong?