PHP gettext function returns only orthogonal untranslated string

I am trying to use gettext to support localization on my site. I followed various gettext configuration guides and did the following:

I created the following files and directories in the root directory of my project:

test.php locale/ de_DE LC_MESSAGES messages.mo messages.po en_GB LC_MESSAGES messages.mo messages.po 

I used Poedit to create the above .po and mo files. I sued the use of Unix line endings, UTF-8 and set the language and country accordingly.

Then I created a PHP script called test.php that has the following code:

 <?php define('LOCALE', 'de_DE'); // Set up environmental variables putenv("LC_ALL=" . LOCALE); setlocale(LC_ALL, LOCALE); bindtextdomain("messages", "./locale"); bind_textdomain_codeset("messages", LOCALE .".utf8"); textdomain("messages"); die(gettext('This is a test.')); ?> 

I imported the text "This is a test." to Poedit and provided the translation and saved it.

But for some reason, the test.php script only outputs the source code, untranslated. He refuses to download the version for translation files.

It is worth noting that the server runs Linux (Ubuntu), Apache 2.2.11 and PHP 5.2.6-3ubuntu4.5. I checked phpinfo() and gettext is enabled.

Can someone help me? Thanks.

+7
php apache localization gettext
source share
3 answers

Your problem may be due to the lack of a locale in your system. Please install German and everything should work:

 sudo apt-get install language-pack-de-base 

Then run the following command and you will see German locales:

 locale -a 

After that, the following code should work if you still have the .po and .mo files in the directory structure that you described:

  <?php setlocale(LC_ALL, 'de_DE.UTF-8'); bindtextdomain('messages', './locale'); textdomain('messages'); echo gettext('This is a test.'); ?> 
+14
source share

Yes, yes, PHP gettext support again. Just a hint that may or may not help you:

Due to the terrible implementation of extext PHP, many open source projects such as WordPress have switched to this one: http://savannah.nongnu.org/projects/php-gettext/ and completely bypass the original version.

I also did this in one of my projects, and I can’t say that I missed something.

Disadvantage for commercial projects: it is under the GPL.

+1
source share

try the following

 <?php define(LC_MESSAGES, 'de_DE'); // Set up environmental variables putenv("LANGUAGE=de_DE"); bindtextdomain("*", dirname(__FILE__).'/locale'); bind_textdomain_codeset("messages", 'UTF-8'); die(gettext('This is a test.')); ?> 
0
source share

All Articles