views:

383

answers:

3

I'm trying to use gettext add localisation support to my website. I've followed various guides on how to setup gettext and have done the following:

I've created the following files and directories in the root of my project dir:

test.php

locale/
  de_DE
    LC_MESSAGES
       messages.mo
       messages.po

  en_GB
    LC_MESSAGES
       messages.mo
       messages.po

I've used Poedit to create the above .po and mo files. I've made sue it use Unix line endings, UTF-8 and set the language and country accordingly.

I've then created a PHP script called test.php which 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've imported the text "This is a test." to Poedit and supplied the translation and saved it.

But for some reason the test.php script will only output the original text untranslated. It refuses to load the version for the translation files.

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

Can someone help me? Thanks.

A: 

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.'));

?>
solomongaby
Unfortunately this did not work. It still just displays the English version.
Camsoft
+1  A: 

Yes, yes, PHP's gettext support again. Just a hint, that might or might not be helpful to you:

Because of PHP's awful gettext implementation, many open source projects like WordPress switched to this one: http://savannah.nongnu.org/projects/php-gettext/ and completely bypass the original version.

I did it, too, in one of my projects, and I can't say that I miss anything.

Disadvantage for commercial projects: It's under the GPL.

Boldewyn
Interesting. I'll have a look at this. Arh though it being GPL is going to be a problem.
Camsoft
Not able to use this because like you said it's GPL and this issue I am having is on a commercial project.
Camsoft
I know the problem. We have here a closed-source project, too, and we are left to implement something ourself, because we can't use php-gettext (or want to rely on PHP's gettext module). However, I thought it's worth an answer, as it's an important project to incorporate in other GPL'ed PHP software.
Boldewyn
+7  A: 

Your problem might be related to a missing locale on your system. Please install the German locale and everything should work:

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

Then, issue the following command and you should see the German locales:

locale -a

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

  <?php

  setlocale(LC_ALL, 'de_DE.UTF-8');
  bindtextdomain('messages', './locale');
  textdomain('messages');

  echo gettext('This is a test.');

  ?>
bpedro
So, did it work? Was it in fact a problem related with a missing locale or something else?
bpedro
Yes! It's worked. Thank you very much for your help.
Camsoft
No problem! I'm glad I could help!
bpedro