tags:

views:

56

answers:

2

Hello, I want to use something like this in a PHP file:

$about = array ('dutch' => 'over', 'spanish' => 'sobre', 'english' => 'about');
if ($USER['lang'] == 'dutch'){
 // $about - becomes 'over' 
   } 
elseif ($USER['lang'] == 'spanish') {
// $about - becomes 'sobre'
  }
else {
// $about - becomes 'about'
}

And transfer the outcome to a HTML file. I was thinking I use {about} in the HTML page to print the outcome.

Does anybody know how to do this???

+1  A: 

You want to set up a multi-language page. The simplest way to access the words would be using an array

if ($USER["lang"] == "dutch")

$words = array("about" => "over",
                 "word1" => "translation1",
                 "word2" => "translation2");

...

echo $words["about"]; // Outputs the correct word for "about"

If it's a bigger project with many words, you may want to look at a full-blown internationalization solution like the Zend Framework's Zend_Translation. It allows you to store the translations in many different formats, including XML files, text files, or a database for speed.

Pekka
echo $words["about"];outputs the correct word for "about" only in php page. I need it to be output on a html page.
martin
Can't be done in pure HTML. You'll either parse your HTML in PHP, or have a PHP script write out the translated HTML.
Pekka
+2  A: 

Using the same code structure that you have currently:

$about = array ('dutch' => 'over', 'spanish' => 'sobre', 'english' => 'about');

if ($USER['lang']) {
    echo $about[$USER['lang']];
} else {
    echo 'about';
}

Make sure that $USER['lang'] is properly sanitised/verified

This solution is only ideal if you have a few words to translate. If you would like to do any more than this then you should investigate using a complete translation library.

Here are several translation libraries that you might like to check out:

Edit: Alternatively, you could use a switch. This means that you don't have to compare $USER['lang'] against a list of available languages.

switch ($USER['lang']) {
    case 'dutch':
        $about = 'over';
    case 'spanish':
        $about = 'sobre';
    default:    //if $USER['lang'] doesn't match any of the
                //cases above, do the following:
        $about = 'about';
}
Pheter
Gracias amigo, I think I use the switch them. But what about getting the output into a html page?
martin
The variable $about contains the word 'about' in the language chosen. To output the contents of variable $about simply use "echo $about" (no quotes) at whichever point in your html that you would like it displayed.You may be interested in using a templating engine such as Dwoo (http://dwoo.org).If my answer solved your problem then I would really appreciate it if you could mark my answer as the accepted solution (click on the tick below the votes for this answer.
Pheter