Brian_15 Posted October 10, 2016 Share Posted October 10, 2016 (edited) Need help with modifying current code to work with en_US instead of en-US and so on. <?php // Languages we support // $available_languages = array("en", "en-us"); # old $available_languages = array("en", "en_US"); # new $default_language = "en"; // a default language to fall back to in case there's no match function prefered_language($available_languages, $http_accept_language) { global $default_language; $available_languages = array_flip($available_languages); $langs = array(); preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', $http_accept_language, $matches, PREG_SET_ORDER); foreach($matches as $match) { list($a, $b) = explode('-', $match[1]) + array('', ''); $value = isset($match[2]) ? (float) $match[2] : 1.0; if(isset($available_languages[$match[1]])) { $langs[$match[1]] = $value; continue; } if(isset($available_languages[$a])) { $langs[$a] = $value - 0.1; } } if($langs) { arsort($langs); return key($langs); // We don't need the whole array of choices since we have a match } else { return $default_language; } } $lang = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]); print "<h3>Site available in:</h3><pre>"; print_r($available_languages); print "</pre>\n<h3>Browser supported languages:</h3><pre>"; print_r(explode(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"])); print "</pre>\n<h3>site will display in: <em>".$lang."</em></h3>"; ?> Edited October 10, 2016 by Brian_15 Quote Link to comment https://forums.phpfreaks.com/topic/302307-http_accept_language-en-us-to-en_us/ Share on other sites More sharing options...
requinix Posted October 10, 2016 Share Posted October 10, 2016 I see two places in that code that deal with hyphens in strings. Try replacing them with underscores. Quote Link to comment https://forums.phpfreaks.com/topic/302307-http_accept_language-en-us-to-en_us/#findComment-1538162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.