Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Localization Question

I'm building a Open Source product and I thinking about Localization, I've read about gettext but there seems to be a lot of problem to get it to work in different systems (servers,os etc).

How would you handle this? Is there a secure way to help gettext work on several systems? Perhaps it already is?

Regards from Sweden / Tobias

like image 626
sandelius Avatar asked Jan 27 '10 17:01

sandelius


People also ask

How to add localization in PHP?

php if (! isset($_GET['langID'])) $lang = 'en_US'; else $lang = $_GET['langID']; putenv("LANG=". $lang); setlocale(LC_ALL, $lang); $domain = "messages"; bindtextdomain($domain, "locale"); textdomain($domain); echo gettext("welcome"); ?>

What is locale PHP?

A "Locale" is an identifier used to get language, culture, or regionally-specific behavior from an API. PHP locales are organized and identified the same way that the CLDR locales used by ICU (and many vendors of Unix-like operating systems, the Mac, Java, and so forth) use.

How many locales are there?

Edit: The original list of locales has been edited to add additional locales that were not included before. Now 228 listed. The most important thing for various versions of English is in formatting numbers and dates. Other differences are significant to the extent that you want and able to cater to specific variations.


2 Answers

I recommend you take a look at Zend_translate, Zend_locale and Zend_Date. I'm only starting to dabble with them myself, but to me, they already look like a really good, clean and modern solution to internationalization, in contrast to the chaos that is gettext.

The introduction to Zend_translate lists a number of strong arguments why to choose it (or something similar) over gettext.

In multilingual applications, the content must be translated into several languages and display content depending on the user's language. PHP offers already several ways to handle such problems, however the PHP solution has some problems:

Inconsistent API: There is no single API for the different source formats. The usage of gettext for example is very complicated.

PHP supports only gettext and native array: PHP itself offers only support for array or gettext. All other source formats have to be coded manually, because there is no native support.

No detection of the default language: The default language of the user cannot be detected without deeper knowledge of the backgrounds for the different web browsers.

Gettext is not thread-safe: PHP's gettext library is not thread safe, and it should not be used in a multithreaded environment. This is due to problems with gettext itself, not PHP, but it is an existing problem.

like image 178
Pekka Avatar answered Sep 28 '22 00:09

Pekka


Zend Framework's Zend_Translate is most flexible what I've seen. It doesn't necessarily need gettext support module in PHP's side as it reads the .mo-binary format itself.

like image 24
raspi Avatar answered Sep 28 '22 00:09

raspi