试试这个,它在PHP中,但是使用相同的正则表达式,我确定它可以适应任何语言:
$langs = array(); // used to store valuesif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // break up string into pieces (languages and q factors) preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)s*(;s*qs*=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); }}
首先产生具有首选语言的排序数组:
Array( [en-ca] => 1 [en] => 0.8 [en-us] => 0.6 [de-de] => 0.4 [de] => 0.2)
从示例
ACCEPT_LANGUAGE标题:
en-ca,en;q=0.8,en-us;q=0.6,de-de;q=0.4,de;q=0.2
这里的工作示例
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)