Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the reasons to use or to not use PHP's native gettext versus a self-build?

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?

like image 917
Gerben Jacobs Avatar asked Mar 25 '11 15:03

Gerben Jacobs


People also ask

What is use of gettext in PHP?

The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications. Translating strings can be done in PHP by setting the locale, setting up your translation tables and calling gettext() on any string you want to translate.

What is localization PHP?

Localization in software engineering is the process of adapting the content of a system to a particular locale so the target audience in that locale can understand it. It is not just about changing content from one language to another.


2 Answers

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.

like image 72
mario Avatar answered Nov 02 '22 02:11

mario


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.

like image 32
Htbaa Avatar answered Nov 02 '22 01:11

Htbaa