views:

255

answers:

4

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

+4  A: 

Don't write your own language framework. Use gettext. PHP has standard bindings that you can install.

Fragsworth
I just glanced over the gettext page earliar today before I posted this, I cannot really make out how it works in a nutshell can you explain a little about it?
jasondavis
jasondavis: it does exactly what you are attempting to do and more. When you install it, you choose somewhere to place your language translations file (".po"). You can translate all your strings in this file. Then in your source, you wrap all your multilingual strings with gettext() (or typically an alias, '_()') and it will pull your current translation of that string for you.It also provides for plural phrases and some other important features that you will find necessary.
Fragsworth
A: 

why not you just make it as multi-dimesional array...such as this

<?php

$lang = array(
    'EN'=> array(
        'NO_PHOTO'=>'No photo\'s avaiable',
        'NEW_MEMBER'=>'This user is new',
    ),
    'MY'=> array(
        'NO_PHOTO'=>'Tiada gambar',
        'NEW_MEMBER'=>'Ini adalah pengguna baru',
    )
);

?>
This'd use more memory than necessary---because this'd be loading *every* language for *every* request. It'd be better to break it into multiple files and load the single appropriate one.
brianreavis
+3  A: 

It'd probably be best to define a function that handles your language mapping. That way, if you do want to change how it works later, you're not forced to scour hundreds of scripts for cases where you used $lang[...] and replace them with something else.

Something like this would work and would be nice & fast:

function lang($phrase){
    static $lang = array(
        'NO_PHOTO' => 'No photo\'s available',
        'NEW_MEMBER' => 'This user is new'
    );
    return $lang[$phrase];
}

Make sure the array is declared static inside the function so it doesn't get reallocated each time the function is called. This is especially important when $lang is really large.

To use it:

echo lang('NO_PHOTO');

For handling multiple languages, just have this function defined in multiple files (like en.php, fr.php, etc) and require() the appropriate one for the user.

brianreavis
FYI, this solution is inferior to Fragsworth's suggestion: `gettext`. Gettext is definitely the better route for this sort of thing. However, this is how you'd do it if you were to roll your own basic system.
brianreavis
good idea
jasondavis
+2  A: 

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

raspi