Reputation: 48973
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
?>
Upvotes: 8
Views: 3714
Reputation: 6690
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.
Upvotes: 1
Reputation: 597321
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]?
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 :-)
Upvotes: 4
Reputation: 143259
There are a few drawbacks in your "implementation".
gettext
it's implemented in php
. ;-)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.
Upvotes: 6
Reputation: 401172
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 :
Upvotes: 13