XHTML.net

Technology talks by Loïc d’Anterroches

News, articles, PHP, scripts, XHTML/CSS, …

  1. Home
  2. PHP: Hypertext Preprocessor

Gettext and PHP, the good, the bad and the hugly

The 2006-12-19 at 15:19 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor.

Yesterday I took a look at gettext to be used in the core of the next version of Plume CMS. I was ready to use it until I discovered some annoying things:

  • You need to have it to use it. Not really a problem as nowadays the gettext extension is installed by nearly all the provider.
  • In some cases, you need to restart Apache to reload the new translations.
  • You need to have the corresponding locale installed on the system to be able to use a given language. That is, if your provider has only the English locale available, sorry, it won’t work.

Check the comments on the gettext PHP manual page to see all the problems you can have. As Plume CMS is supposed to be used in a non controlled environment, I had to find another solution. Looking around, I found a nice gettext benchmark. The results from the benchmark together with the interesting comments can be summarized as:

  • the pure PHP implementation of gettext solving the problems is slow.
  • an array with translation strings is nearly as fast as gettext.
  • loading the translation strings from a .ini like file is convenient, but very slow.

The solution I found is basically to reuse the approach of the current Plume CMS translation system and extend it. At the moment, the translation system does not support plural forms, so I created it. The partial implementation is available in Pluf. Pluf is the MVC framework that will be used as the core of the next release of Plume CMS. Now you can do the following:

// Translate a single string
echo __('Hello World!'); 
// Translate a plural form
echo sprintf(_n('Here is %d apple.', 'Here are %d apples.', $n), $n);

The file storing the translations is an easy to read and write .ini like file and the framework will automatically cache the strings in a dynamically generated PHP file ready for inclusion and parsing by the PHP tokenizer, thus really fast.

Here is an example of a .lang file:

# -*- coding: utf-8 -*-
# (C) 2004-2006 Cédric Arrabie, Loïc d'Anterroches
;Welcome on the Plume CMS installation
Bienvenue sur l'assistant d'installation de Plume CMS

;Next
Suivant

;Nothing to see here.
p;You have some things to see here.
0;Rien à voir ici.
1;Une chose à voir ici.
2;Plusieurs choses à voir ici.

Basically, we will be able to use it like gettext and ngettext. Of course it is a very simple approach and I am not an expert in internationalization, but today Plume CMS is already translated in several languages and this approach is just improving the current approach, so basically translators will be happy (I hope).

Comments from readers

Voice your ideas

It is painless and I try not to kill electrons in the process.


Your email is required but will not be shared nor displayed.


Do you think your comment will force me to write even better stuff next time? If so, you simply rock.


Logo of Plume CMS