Dernière modification : 2008/03/07 12:29

Studying use of gettext for WiKiss internationalisation (FS#37).

Gettext assets

  • used system, very efficient
  • most hosting have it ?
  • easy for translators (lot of tools availables)
  • automatic treatment of missing messages

Gettext problems

  • hours to understand how it works :)
  • gettext pgp extension must be installed
  • the concerned locale must be installed in the hosting system
  • .mo files are system dependents (linux/windows)
  • the locale name differs from the language code (en, en_US)
→ test which locales are installed on some hosting platforms

Installed locales

Here's a list of hosting providers and their installed locales.

For listed linux distibutions, I used standard install, without any configuration.

→ See up-to-date table in french version

Under linux, the command local -a will be more verbose.

Test script

This php script test installation of some locales. To use it, you just have to copy it to your hosting platform and display it in your web browser eg:
Then it should be great if you can copy the result and send it to me on the WiKiss Listes with the name of your hosting provider.

   $testlangs = array('fr','fr_FR','fr_BE','fr_CA','en','en_US','en_GB','es','es_ES','de','de_DE','nl','nl_NL');

   function testlocale($lang)
      $dir=setlocale(LC_ALL, $lang);
      if ($dir == $lang)
         return "<li>$lang : OK</li>\n";
         return "";

   if (extension_loaded('gettext'))
      echo 'gettext is installed. Testing some locales ...<br/><ul>';
      foreach ($testlangs as $lang)
         echo testlocale($lang);
         echo testlocale($lang.'.utf8');
         echo testlocale($lang.'.iso885915');
         echo testlocale($lang.'.iso88591');
      echo '</ul>';
      echo 'gettext is not installed :(';

If you want to test some more locales, just had them to the testlangs array.


Here's a minimal code exemple :
// Choose language
$lang = 'en_US.utf8';
$dir=setlocale(LC_ALL, $lang);
echo 'setlocal:'.$dir."<br/>"; // if string is empty, the locale is not installed and gettext won't work :(
// Specify place of translation tables
bindtextdomain("$domain", "./locale");
// Choose domain
echo gettext('Aide');
// should display Help :)

With this code, the locale file must be : ./locale/en_US/LC_MESSAGES/

To create .po file from the php code :
xgettext --language=PHP --default-domain=messages --from-code=UTF-8 index.php

To compile the .mo file :
msgfmt -o locale/en_US/LC_MESSAGES/ locale/en_US/LC_MESSAGES/messages.po