I have been searching for a while, but can't seem to find the correct way to achieve this:
For creating a multilanguage(2) website, I was thinking of using the following folder structure:
DOMAIN
www.domain.com/index.php
NL-folder
www.domain.com/nl/index.php (alternative home.php?)
www.domain.com/nl/pagina2.php
...
EN-folder
www.domain.com/en/index.php (alternative home.php?)
www.domain.com/nl/page2.php
...
I have read that some people find the www.domain.com/index.php
being a language select page annoying.
Therefore I was thinking this page should show the same content as the index/home page in the preferred language. I have heard this can be done by using the headerfield Accept-Language
. Alternativly, I should show the content of www.domain.com/nl/index.php
or redirect there(To be able to do language-dependent queries I feel like I have to redirect www.domain.com/index.php
to a language specific page, so that the language can be selected?).
Ofcourse the user would be able to change language later...
I am completely stuck on how to correctly achieve this.
The only thing I might come up with is redirecting. With the code found on http://www.thefutureoftheweb.com/blog/use-accept-language-header this would become:
$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
// create a list like "en" => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
// look through sorted list and use first one that matches our languages
foreach ($langs as $lang => $val) {
if (strpos($lang, 'nl') === 0) {
header("Location: http://domain.com/nl/index.php");
} else if (strpos($lang, 'en') === 0) {
header("Location: http://domain.com/en/index.php");
}
}
Is my example ok, or should I do it differently?