Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i18n with gettext but without the locale hassle?

I am looking for a standards-compliant way to store multi-language content for a Web Application. Until now, I have employed my own "translate()" functions that read data from a file or a dictionary table in a database. However, keeping the dictionaries up to date in a database table is very cumbersome if you work with different copies of the web app.

I like gettext because there is a multitude of tools available for it. However, I develop applications for different platforms. I am absolutely unwilling to deal with the crappy ways of setlocale() and consorts - namely the dozens of different locale string variations differing from system to system that you need to provide for to get it working. Never ever. I have a set of languages - say de, en, and es - and I want to load the appropriate dictionary and work with _() without touching setlocale() or bindtexdomain() once.

Is this somehow possible using gettext? Or does somebody know another simple, small, fast (!) i18n solution for PHP that can work with .po/.mo files, preferably without requiring a PHP extension?

like image 401
Pekka Avatar asked Oct 25 '09 11:10

Pekka


2 Answers

This method should work even with non-standard locales:

$locale = 'someWeirdLocale';

putenv('LC_MESSAGES=' . $locale);
bindtextdomain('domain', './locale');
textdomain('domain');

Enjoy!

like image 54
Alix Axel Avatar answered Nov 09 '22 11:11

Alix Axel


Zend_Translate works with it

http://framework.zend.com/manual/en/zend.translate.adapter.html#zend.translate.adapter.gettext

like image 33
Valentin Golev Avatar answered Nov 09 '22 11:11

Valentin Golev