Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is so good about gettext for language files?

Tags:

php

gettext

Everywhere on SO when it comes to making a multi language app in PHP everyone say that gettext is the best way to do it. I am just wanting to know why?

Like what makes this method below, less efficient then using gettext?

<?PHP
//Have seperate language files for each language I add, this would be english file
function lang($phrase){
    static $lang = array(
        'NO_PHOTO' => 'No photo\'s available',
        'NEW_MEMBER' => 'This user is new'
    );
    return $lang[$phrase];
}
//Then in application where there is text from the site and not from users I would do something like this
echo lang('NO_PHOTO');  // No photo's available would show here
?>
like image 901
JasonDavis Avatar asked Sep 12 '09 17:09

JasonDavis


2 Answers

A good thing with gettext is that it is kind of a de-facto standard : it is used by lots of applications, in many languages -- which means many people work with it.

Another good thing (maybe a consequence, or a cause, or both, actually) is that several tools, like Poedit for instance, exist to edit gettext files : you don't have to go through some PHP (or whatever) source-code. And this is very important :

  • it means non-technical people can edit gettext localisation files
    • maybe this doesn't seem important to you now,
    • but if you are working on a big open-source application, it'll become important if it is successful and many people need it in their own language.
  • it means they don't risk to break the application (think "parse error" because of a mismatch in quotes, for instance ;-) )
like image 103
Pascal MARTIN Avatar answered Oct 20 '22 00:10

Pascal MARTIN


There are a few drawbacks in your "implementation".

  • Unlike gettext it's implemented in php. ;-)
  • It keeps the whole translation in memory no matter if you use it or not
  • Which is worse, it instantiates the whole array of data whenever you need one line (it does take a while and quite some memory in PHP)
  • It's unable to handle plural forms for many languages
  • The code, using this translation is hardly readable
  • The code, using this translation has no embedded fallback to use in case of absent translation.

Basically, your implementation is widely used in many projects who are dying to claim their internationalization support and highly innovative invention of the wheel, but don't care a bit about the result and do not know the good from the bad.

like image 29
Michael Krelin - hacker Avatar answered Oct 20 '22 00:10

Michael Krelin - hacker