Parse a Value Outcome to HTML
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?
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:
- Zend Translate
- Translation2 and guide
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';
}
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.
精彩评论