views:

288

answers:

4

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
?>
+5  A: 

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 ;-) )
Pascal MARTIN
So as far as performance goes, you thing using a function with an array or constants for language files would be as fast or as good on performance as using gettext? My app is somewhat large but it will always be a private project where I am the only one who needs to edit it, for me it would be easiar to not have to set up gfettext and make sure the server has it installed and running. But if there is ANY performance gain from using gettext then I would probably put more effort into going that route.
jasondavis
I don't really know about performances (this : http://mel.melaxis.com/devblog/2006/04/10/benchmarking-php-localization-is-gettext-fast-enough/ might be useful ? ) -- but your implementation has several others problems that what I said in my answer ; for instance : no plural, a whole big array to parse and keep in memory
Pascal MARTIN
THANK YOU that link is exactly what I have been looking for, it is kind of old but still what I want, a benchtest without me having to do it (only because I don't know how to set up gettext yet)
jasondavis
About performance: .po files are converted to .mo files which are in binary and really fast to read.
raspi
The link you provided, the guy actually included his benchmark code for 3 different methods, I ran it on my machine, turns out my dev server already had gettext installed and working, looks like I have a solution now! thnaks again +5 if I could
jasondavis
@jasondavis : you are welcome :-) Have fun !
Pascal MARTIN
Don't forget plural forms, they are great!
Alix Axel
+3  A: 

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.

Michael Krelin - hacker
+2  A: 

It's the same answer that for all the questions looking like :

What is better with [hightly proved and vastly used solution] than with [my hacky cocky noob implementation]?

  • It's been here for a long time, an believe it or not, if experienced dev worked on it, used it and praised it, it's probably for a reason.
  • It's a standard, and so even if your solution would be better, the price to pay for breaking it may not be worth it.
  • It can do 1000 more than your implementation because it's developed with a global scope in mind whereas your idea only aims to solve your problem.

I am not criticizing, we all did that, trying to convince ourself that we are smarties and others are overkill programmers. That's part of the way to learn. I actually continuously do that, reinventing the wheel or showing off with my friends / colleagues about some KISS code I hacked. With time, you just end up doing it less and less, and I suppose you stop doing it the day when you would actually deserve to do it :-)

e-satis
my hacky cocky noob implementation in this case is used in several large OPEN SOURCE applications though, in fact I see both methods used often and I am trying to figure out which is the best route without installing gettext and doing a benchmark if someone else has already done it.
jasondavis
PunBB is OpenSource and started with one of the worst code base ever known. I started an open source project in my first years of programming. Making mistakes and having a big ego as just nothing to do with open source, its just something we all do. Don't take it personnally, be happy to know that you're progressing ;-)
e-satis
And BTW, you are the one having 'Premature optimization is not the root of all evil' as a quote in his profile. Isn't time to appy it ?
e-satis
I did end up using the gettext extension, I never had anything against it, I was just trying to find out the pros and cons of both instead of just going with the flow and using what people say is the way to go without anything backing up the reasoning
jasondavis
A: 

The advantage of gettext, compared to other solutions like yours, Java string tables or Windows resources, is that it makes the source code more readable.

Compare this:

printf(_("No photo available (error %d)."), err);

with this:

printf(i18n(NO_PHOTO), err);
// or some variant of the same thing

In the first case, you can see the message right there in the code and you know exactly what it does. In the latter case, you only see a symbolic constant and have to look up the exact text and format specifiers.

vslavik