I do some research to make my web site multi-language with PHP 5 but I can't found any things. So anyone have a idea how to make your website multi-language?
An easy way to do it would be with multiple files, some of them being the language files, holding arrays with the language keys. Something like
lang_LANGCODE.php
$language = array (
"someMessageID" => "The visible text of the id",
"anotherMessage" => "The visible text of another message",
);
And add the rest of messages inside the same array, see Array Documentation.
Then in your PHP file you would do something like
if ($languageid == "english") {
include("path/to/lang_en.php");
}elseif($languageid == "spanish"){
include("path/to/lang_es.php");
}elseif($languageid == "other supported") {
//Do the same with the rest of languages
}else{
// Always load a default language file if the requested language don't exist
include("path/to/lang_en.php");
}
And to use the messages you should use $language['messageID']
in your code.
If you want to get the user language, you could try to get if one of your supported languages is in the array of the Accept-Language
header. Else, you could simply ask it and save it in a cookie, database or session variable.
Make sure that you have all strings in all your files or empty strings and errors may appear. There are methods to do fallbacks, but it's a bit more complex.
You could also do all checks in the language file and define the array conditionally instead of including a file conditionally, but if you have a lot of messages it's better to read if you have language strings in separate files.