1

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 to de.
  • I tried using LANG instead of LC_ALL.

None of that made a difference.

What am I doing wrong?

dodov
  • 5,206
  • 3
  • 34
  • 65
  • 1
    To the person who downvoted my question - is it unclear? Did I not provide enough information? Please, explain what I did wrong. – dodov Sep 21 '17 at 12:50
  • 1
    For what's it's worth, I recently gave up on _gettext_ and moved onto simple array based translation. _gettext_ was way too complicated for what I needed - converting 'greeting' into 'Hello', 'HALLO', 'Howzit' for different locales. – waterloomatt Sep 21 '17 at 12:59
  • 1
    Simple example - http://sandbox.onlinephpfunctions.com/code/301ae624da5084afd881ae95303e38feefca4893 – waterloomatt Sep 21 '17 at 13:54
  • I have way too many text and pages. This won't work well for me. It was the first thing I considered doing. – dodov Sep 21 '17 at 14:31
  • @waterloomatt your example is wrong, don't use gettext – stackdave Mar 26 '18 at 17:21
  • Get text does not work for me either. I disable the php gettext extension and use 3 files from here. https://sources.debian.org/src/php-gettext/1.0.12-0.1/ – MJ Khan Mar 27 '18 at 08:00

0 Answers0