tags:

views:

688

answers:

10

Hi friends I would like to develop a multilanguage page in PHP, for exemple english/german/japanese.. so when i click on german the page language will change to german, then i click english it change into english. Please guide me to do this . Thanks in advance....

+2  A: 

You could use sessions to store what language the user has set.

$_SESSION["lang"] = "en";
$_SESSION["lang"] = $_GET["lang"]; 
// or this way, then you can do example.com/?lang=en

Then create a file with an array of the translation.

$lang["en"]["welcome"] = "Welcome to the site";

And then another file could have the German translation

$lang["de"]["welcome"] = "Willkommen auf der Webseite";

And then you would just call the $lang array with the session

echo $lang[$_SESSION["lang"]]["welcome"];

This is a simplified version of this, you could also use objects to store the translation. But what i do not recommend is to use a database to store the translation, since this involves either a very large SELECT or very very many.

Ólafur Waage
In the last echo don't you mean: $lang[$_SESSION['lang']]['welcome'];
Pim Jager
Actually i need to change all the contents in that page . Just like http://www.google.co.in/
Sakthivel
@sakthiopr websites are usually translated in this way (or some way similar to this) It's just a collection of strings that need to be outputted at a certain location.
Ólafur Waage
A: 

I personally translate my complete views. Thus you would have a file structure like:

application/
 someScript.php
views/
 de/
  someView.php
 en/
  someView.php
models/
 someModel.php

And then as suggested by Olafur store the language in a session variable and do something like this.

 session_start(); //dont forget to call this somewhere
 $_SESSION['lang'] = 'en';
 //and then in somewehere else:
 include 'views/'.$_SESSION['lang'].'/someView.php'; //or whatever method is used to include views.

EDIT
You should seperate you output from your script. Thus you would have a script which does some scripting stuff, like getting the currect UNIX timestamp, this would be someScript.php in the example above:

  //someScript.php
  $timestamp = time();

Now we could add all the output directly below this, but that would maintainability very hard, so we do the output in a different file, which here would be the someView.php files. So the someView.php in the 'en' folder would be:

<html>
<head>
<title>Currect UNIX timestamp!</title>
</head>
<body>
<p>The current UNIX timestamp is: <?php echo $timestamp; ?> seconds</p>
</body></html>

And in the 'de' folder it would be practicly the same file but with all the text in german.

Now all we need to do is include these output files in our someScript.php thus we add the following line to the bottom of that script:

 include '../views/'.$_SESSION['lang'].'/someView.php';

You should read more on MVC here: http://www.onlamp.com/pub/a/php/2005/09/15/mvc_intro.html

Note: That this example isn't strict MVC but this should give you the idea.

Pim Jager
i can't get you . Please explain little bit elaborate
Sakthivel
But, when you need to edit a view, you must edit a view for each language ? That doesn't sound very pragmatic
I.devries
Gregor
In my app we do this this way because the different languages have slightly different views. In this example just having an array of languages would be more easy.
Pim Jager
A: 

Most frameworks will have helpers that will assist you in this. Here is Codeigniter's, and cakePHP's, for starters.

John McCollum
+6  A: 

We use the http://php.net/manual/en/intro.gettext.php way. Works fine and we all worked already with http://www.gnu.org/software/gettext/manual/gettext.html in other projects (non PHP projects). So it was easy to start.

I think the right way depends on who translates the text! If it's the developer(s) you can have for each language an own view. But if you have some other people translating the text, something like gettext (translation is in a dedicated file and there are tools to work with this kind of files) the right choice!

Gregor
Gettext is absolutely the way to go. It makes it much easier for a third party to translate your website.Basically, you gettext-ise your website, send a .po file off to a translator, and they give you back a .po file that contains all the translated strings in it. No code modification.
Jeremy Visser
A: 

Usually you'd need a UTF-8 enabled website. This is set via the header of the file. (and can be set via header()) function.

Then I'm guessing you want to have the site in English and Chinese. Then I'd recommend to have a language file. Perhaps an array or an object where you store the paragraphs you want to display on the site. And then you just print out that paragraph.

Then to know what language the user is asking for, I'd use $_SESSION to store the user's chosen language.

Then your language file could look like this

$lang["en"]["Welcome"] = "Welcome to the site";

And then the Chinese welcome file would have something like this

$lang["ch"]["Welcome"] = "歡迎";

Then in the location you want to print this out. You would do

echo $lang[$_SESSION["lang"]]["Welcome"];
Ólafur Waage
for a page greater than 40 pages including multiple dynamic pages that would take a long time. i think its best to seperate the languages with directories. so website.com/english would be english website.com/chinese would be another
I think that info might have been well served in the question :)
Ólafur Waage
A: 

You can just type the content portion of your HTML in whatever character set you'd like. Just be sure to set the charset and content encoding properly. For example, set it to UTF-8 for simple unicode.

Naaff
A: 

Rails handles it well. You might want to check it out for ideas on how to do this.

jayd
+5  A: 

Strangely, so many php developers seem to think that all natural languages have the same grammar. Making a translation framework without parameters is simply not possible. While the string $lang['welcomeTo'] $sitename might translate correctly for most languages (Welcome to stackoverflow), it fails for turkish for example where it should be $sitename $lang['welcomeTo'] (stackoverflow'a hos geldiniz) or for german where either way would be possible (Willkommen auf stackoverflow/Stackoverflow heißt Sie willkommen). And this gets worse when you try to translate sentences with multiple variables.

The one and only option is to use parameterized translations (say: functions)

Language::getCurrentLanguage()->translate('welcomeTo', array('sitename' => $sitename));

# Translation file for english (configfile-style):
welcomeTo=Welcome to $sitename

# Translation file for turkish:
welcomeTo=$sitename'a hos geldiniz

# Translation file for german:
welcomeTo=Willkommen auf $sitename
# Or
welcomeTo=$sitename heißt sie willkommen

In short: If you want real i18n, use functions to retrieve your translations, not variables. You might want to look into the intl extension of php, too.

soulmerge
+1  A: 

One effective way of building multi-language websites is by not using hard coded labels but retrieving them from the appropriate language file, because basically what you need to do is to orient different users on your site. If you had data displayed which you store on a database maybe you should consider adding some database tables storing translations in different languages. To do some of the work in C# for example you can use Resource Files storing label values.

Izabela
A: 

As the Gregor said; gettext is the proffesional way for me...

here is a good article link

But i want to add some important points

I- With this method u will have to use .po and .mo files if you create a po file under windows, you can't use it under a linux. Or .po file that created under linux can't bwe used under windows.

II- linux can create .po files itself but in windows i suggest you to use a program such as poedit (which u can get it from its site: http://www.poedit.net/ )

III- Some locales have character problems. But i don't remember which one are they?:(

H2O