PHP Detect User language -
i need script like
if ($userlanguage === 'english') { echo "we have detected english. visit our site in english?"; } else { header('location: /index.php?lang=default'); } the script above used example. have looked on google , gave me geolocation scripts , such. don't want third party url in script. never know if service goes down or not.
where find this?
i'm not going repeat valid answers here, this link shows approach (for multi-lingo websites) taking advantage of http_negotiate_language() method
so combining mapping , exciting code have:
$map = array("en" => "english", "es" => "spanish"); $userlanguage = $map[ http_negotiate_language(array_keys($map)) ]; if ($userlanguage === 'english') { echo "we have detected english. visit our site in english?"; } else { header('location: /index.php?lang=default'); } but if interested detect english (en) language, might want shorten script to:
if ('en' == substr($_server['http_accept_language'], 0, 2)) { echo "we have detected english. visit our site in english?"; } else { header('location: /index.php?lang=default'); }
Comments
Post a Comment