6

To make my application multilingual I'm wondering if there are big advantages to GNU's gettext or if there are big disadvantages of building your own 'library'.

Also if 'build your own' is more advised, what are the best practices? Obviously they have to be stored in the database, I doubt I wanna work with flat files, so at some point I'm better off caching them, how should I go about this?

Gerben Jacobs
  • 4,515
  • 3
  • 34
  • 56

2 Answers2

5

The gettext extension has some quirks.

  • It keeps translation strings in memory, and thus can necessitate a restart (under the mod_php runtime that is) when catalogs are updated.
  • The gettext API wasn't really designed for web apps. (It looks for environment variables and system settings. You have to spoon feed the Accept-Language header.)
  • Many people run into problems setting it up.
  • On the other hand there is more tool support for gettext.

You will almost always have less trouble with a handicrafted solution. But that being said, the gettext API is unbeatable in conciseness. _("orig text") is more or less the optimal interface for translating text.

If you want to code something up yourself, I recommend you concentrate on that.

  • Use a simple function name. In lieu of _() a few php apps use the double underscore __(). Don't adopt any library that makes it cumbersome to actually use translated strings. (E.g. if using Zend Framework, always write a wrapper function.)
  • Accept raw English text as input. Avoid mnemonic translation keys (e.g. BTN_SUBMT)
  • Do not under no circumstances use the database for translation catalogues. Those texts are runtime data, not application data. (For a bad example see osCommerce.)

You can often get away with PHP array scripts lang/nl.php containing nothing but $text["orig english"] = "dutch here";, which are easy to utilize from whatever access method you use.

Also avoid pressing everything into that system. Sometimes it's unavoidable to adopt a second mechanism for longer texts. I for example used template/mail.EN.txt for bigger blobs.

mario
  • 144,265
  • 20
  • 237
  • 291
  • I agree. On the other hand, gettext is almost the ''de facto'' standard regarding the tools that benefit from it : PO editors, xgettext and msgmerge are really, really handy tools when it comes to managing translations. – Artefact2 Mar 25 '11 at 16:19
  • Thanks for your elaborative answer, one question though. Why are indexes like "BTN_SUBMIT" wrong? – Gerben Jacobs Mar 25 '11 at 16:20
  • @Gerben Jacobs: They are not wrong per se. But looking back, I wish I had avoided this scheme. It's a quite typical PHP approach to `define("TEXT_BTN_NAME", "Button Name")`. But that makes it much harder to later transition to other systems, and foremost makes the source code a bit less readable. Using abbreviations is often based on the false assumption that you want to change text content all the time. In practice that's not the case. Write your app, and translate it at the end. It's simpler to do that all at once and not gradually. But otherwise there's no real technical problem with ABBRV. – mario Mar 25 '11 at 16:44
  • I prefer translation keys because when you want to change the English translation, you have to replace it in a LOT of places. Also, translation keys can contain meta information about the purpose/intended use of the translation string. – Blaise Aug 13 '11 at 19:43
1

Gettext is not thread-safe.

Before deciding to implement your own I suggest you take a look at Zend_Translate. It has native support for a gettext adapter, as well as TMX, CSV, INI, Array and more formats. It should also be easy enough to write your own adapter if your preferred format isn't supported, such as database storage.

Htbaa
  • 2,319
  • 18
  • 28
  • FYI: `setlocale` and hence `gettext` isn't thread-safe because environment variables are not thread-safe. Sounds like a good idea for adding a new proxy-environment API though. – Steve-o May 06 '11 at 16:16
  • 2
    gettext is not thread safe but it doesn't matter in many cases: http://stackoverflow.com/questions/1646249/php-gettext-problems-like-non-thread-safe/6726570#6726570. Zend-translate is nice, but fat and definately way slower than PHP's built-in module. – Stann Jul 17 '11 at 20:45