Hi! Im a hobbycoder that is trying to build his first multilangual site
I use this piece of code to detect users language. If you havent chosen a language it will include your language file based on HTTP_ACCEPT_LANGUAGE i dont know where it gets it from tho..
session_start();
if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'en') $_SESSION['lang'] = "en";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'sv') $_SESSION['lang'] = "sv";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'pl') $_SESSION['lang'] = "pl";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'fr') $_SESSION['lang'] = "fr";
include('languages/'.$_SESSION['lang'].'.php');
It works for me and includes the polish lang file. But is this code accurate? Or is there another way?? How do you think YouTube does this for example?
Would be great if some english, french, swedish or polish folks could visit my site and see if it includes the right file! So i know that it doesnt just work for me :) http://jorm.homeftp.org/
Anyway you think I could improve my code? it it will looks messy as i add more languages with all those elseif!
Thanks