views:

279

answers:

4

Recently I am working into the ability of translating a PHP web application from one language to another. Well, most of those I read involves having language files, then display the selected one like this:

en.lang.php:

<?php

$_TEXT = array();
$_TEXT['welcome'] = 'Welcome to My Application';

?>

fr.lang.php: // french (i translated that with Google =x)

<?php

$_TEXT = array();
$_TEXT['welcome'] = 'Bienvenue sur mon application Web';

?>

I would like to ask if there's any better workflow or algorithm than this? Because value may be inserted into the text and so on - pretty hairy situation. Any help thanks!

Also to note that: this application must work cross-platform (or I should say platform-independent) as such no additional extensions are required based from PHP 4.4.2

+3  A: 

I would advise using gettext, it'll make your life so much simpler...

The gettext functions implement an NLS (Native Language Support) API which can be used to internationalize your PHP applications. Please see the gettext documentation for your system for a thorough explanation of these functions or view the docs at » http://www.gnu.org/software/gettext/manual/gettext.html.

brianreavis
A: 

Checkout PHP's gettext: http://www.php.net/manual/en/intro.gettext.php

If you want to roll your own simple solution, I define a function, __($native, $var1, $var2, ...), that takes the native string and performs the translation + variable substitution for you. The actual implementation of __ (two underscores) will depend on you, but typically $native is the key in your array above and it uses sprintf to substitute.

carl
able to produce some examples? i'm a little confused over at the manual.
thephpdeveloper
A: 

That s actually called Internationalization or localization.

If you are using php, i d recommend you to use smarty. You can do the same thing as you do with Java. and smarty has gettext plugin. you actually dont even have to mess with gettext, you can do it way easier than that.
http://www.smarty.net/

or perhaps you can check out the pear libraries i ve seen packages for localization.

http://pear.php.net/packages.php?catpid=28

A: 

gettext, as suggested by several fellow SOers is an excellent solution. It's not php specific, it's quite popular and as such several tools to simplify translation are available.

And last but not least, keeping the text in your default language in the source is many times easier to work with than having to remember the constant values for texts.

fvu