JasonDavis JasonDavis - 1 month ago 8
PHP Question

Most efficient way to do language file in PHP?

Questions Updated instead of making a new question...

I really want to provide a few alternative languages other then English on my social network site I am building, this will be my first time doing any kind of language translation so please bear with me.

I am researching so I am al ear and open to ideas and I have a lot already here is are the questions.

1)

What does i18n mean, I see it often when researching language translation on SO?

2)

Most people say use gettext PHP has an extension or support for it,

well I have been researching it and I have a basic understanding of it, as far as I can tell it is a lot of extra work to go this route,

I mean coding my site to use it's functions ie; _('hello world i'm in English for now') or else gettext('hello world i'm in English for now') is no problem as any route I go will require that.

But then you have to install gettext on your server and get it working,

then use some special editors to create special files and compile them I think?

Sounds like a pain, I understand this is supposed to be the best route to go though, well everyone seems to say it is.

So can someone tell me why this is the route to go?

3)

I really like the simplicity of this approach, just building a language array and calling the phrase you need in a function like the example below
, you would then just include a file with the appropriate language array.

What I really want to know is, would this be the less better performance method on a high traffic and fairly large site compared to using gettext and if so can you explain why please?

<?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
?>


* some code used from brianreavis's answer below

Answer

Don't reinvent the wheel. Use for example gettext or Zend_Translate.