Asked  8 Months ago    Answers:  5   Viewed   33 times

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

 Answers

10

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.

Wednesday, March 31, 2021
 
Hexaholic
answered 8 Months ago
51

this is what im doing in my cms:

  • for each plugin/program/entity (you name it) i develop, i create a /translations folder.
  • i put there all my translations, named like el.txt, de.txt, uk.txt etc. all languages
  • i store the translation data in JSON, because its easy to store to, easy to read from and easiest for everyone to post theirs.
  • files can be easily UTF8 encoded in-file without messing with databases, making it possible to read them in file-mode. (just JSON.parse them)
  • on installation of such plugins, i just loop through all translations and put them in database, each language per table row. (etc. a data column of TEXT datatype)
  • for each page render i just query once the database for taking this row of selected language, and call json_decode() to the whole result to get it once; then put it in a $_SESSION so the next time to get flash-speed translated strings for current selected language.

the whole thing was developed having i mind both performance and compatibility.

in your case a row in such file could be like:

in en.txt

{"id":"LNG_1","str":"My word"}

in de.txt

{"id":"LNG_1","str":"Mein Wort"}

The current language could be stored in session like $_SESSION["language"]; and use that as starting point. Then, you can read translations like:

lang("LNG_1");
Wednesday, March 31, 2021
 
adjco
answered 8 Months ago
46

Ok, I basically ended up writing a mo file parser based on Zend's Gettext Adapter, as far as I know gettext is pretty much reliant upon the locale, so manually parsing the .mo file would save the hassle of running into odd circumstances with locale issues with setlocale. I also plan on parsing the Zend Locale data provided in the form of xml files.

Saturday, May 29, 2021
 
superhero
answered 5 Months ago
90

I've found out that the locale is not set correctly although PHP reports it does. There is a bug in PHP that is causing this: https://bugs.php.net/bug.php?id=66265

As a result gettext always uses system default locale for translations (after creating translations with my system locale it actually works). Related StackOverflow question: Gettext will always use system default locale

Although the bug is closed and it should somewhat partially work I wasn't able to get it running. Instead I'm now using PHP solution that reads gettext .po files and that works: php-gettext (https://launchpad.net/php-gettext)

Saturday, May 29, 2021
 
Exoon
answered 5 Months ago
75

A lot of CMS uses the array version. I have seen GetText as well for scalable applications. The array version is simpler especially when you want to manage the translation from a web interface.

It is a matter of preference of course.

Saturday, May 29, 2021
 
steros
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share