Multilingual website and browser language
# December 25, 2023, php
The proposal is to create a multilingual website, using a main domain in .COM format accompanied by respective national domains for each language (.CZ, .PL, .DE, etc.).
The aim is for all national websites to redirect to the .COM domain, serving as the "primary" version in English. When a visitor lands on the .COM site with a browser language different from English, a popup should appear, offering the national website in their chosen language.
The first step involves redirecting all national websites to the main website. For instance, to redirect a .CZ website using mod_rewrite:
RewriteCond %{HTTP_HOST} ^website.cz$ [OR] RewriteCond %{HTTP_HOST} ^www.website.cz$ RewriteRule (.*)$ https://www.website.com/$1 [R=301,L]
Then, detect a visitor language:
$visitorLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); $allowedLanguages = ['en', 'de', 'fr', 'it', 'cs', 'sk']; if(!in_array($visitorLanguage, $allowedLanguages)) $visitorLanguage = 'en'; define('VISITOR_LANGUAGE', $visitorLanguage);
CCompare it with the current website's locale (for instance, utilizing the $websiteLocale
variable initialized from a cookie/URL fragment):
if($visitorLanguage != $websiteLocale) { // ... show a message to the visitor // offering a redirect to website with his language }