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?
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.
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.
The gettext extension has some quirks.
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.
_()
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.)BTN_SUBMT
)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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With