i18n with gettext but without the locale hassle?
Asked Answered
V

2

7

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?

Viquelia answered 25/10, 2009 at 11:30 Comment(3)
It is not practically possible to use gettext in PHP without using gettext extension, but don't worry this is quite a common/standard extension of PHP and most installation do have it already activated.Lindley
@Sorin: It is possible, there is a native PHP implementation with most, if not all, the gettext functions.Somerset
@votingmeisfree wordpress and cakephp, to name a few, use po and mo files without using gettext implementation. They open the file directly.Steady
M
2

Zend_Translate works with it

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

Miss answered 25/10, 2009 at 11:52 Comment(3)
And it doesn't require the gettext extension :) They also have adapters for more friendly sources - array, xml, csv etcHimeji
Thanks, this looks interesting. I have been looking and Zend for quite a while and maybe I will try it out. I would still prefer a lightweight stand-alone solution, if anybody knows one.Viquelia
Oh, it's more lightweight than it looks like. You can use only classes you need: for Zend_Translate, you need only Zend_Translate, Zend_Locale, Zend_Loader and Zend_Exception (maybe I'm wrong and you'll need more or less, just try it out)Miss
S
3

This method should work even with non-standard locales:

$locale = 'someWeirdLocale';

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

Enjoy!

Somerset answered 25/10, 2009 at 19:30 Comment(0)
M
2

Zend_Translate works with it

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

Miss answered 25/10, 2009 at 11:52 Comment(3)
And it doesn't require the gettext extension :) They also have adapters for more friendly sources - array, xml, csv etcHimeji
Thanks, this looks interesting. I have been looking and Zend for quite a while and maybe I will try it out. I would still prefer a lightweight stand-alone solution, if anybody knows one.Viquelia
Oh, it's more lightweight than it looks like. You can use only classes you need: for Zend_Translate, you need only Zend_Translate, Zend_Locale, Zend_Loader and Zend_Exception (maybe I'm wrong and you'll need more or less, just try it out)Miss

© 2022 - 2024 — McMap. All rights reserved.