我创建了一个函数,它应该根据用户的语言环境返回最佳翻译。
输入是一个数组(来自BDD),用于翻译多种语言的相同文本,该语言存储为如下关键字:
$textes = array(2) {
["fr-fr"]=> string(24) "Editer l'item"
["en-en"]=> string(22) "Edit the item"
}
当$textes
只包含一个项目时,该函数返回该项目,否则它将返回最佳翻译;我尝试处理intl的Locale函数,但它总是返回一个空字符串:
private static function getMeilleureTraduction(array $textes) {
if (count($textes) === 1) {
return array_pop($textes);
}
// Returns "fr,en;q=0.8,fr-fr;q=0.5,en-us;q=0.3"
$a = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// Returns "fr"
$b = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
// Returns ""
$c = Locale::lookup(array_keys($textes), $a, true);
// Returns ""
$d = Locale::lookup(array_keys($textes), $b, true);
// return $textes[$bestLocale];
}
所以lookup
函数对我没有帮助,因为它无法做出决定。我用错了吗?或者我可能不理解php-intl
的功能目标,我应该自己编写代码?
答案 0 :(得分:1)
最后找到了一个工作函数HERE。
功能:
<?php
/*
determine which language out of an available set the user prefers most
$available_languages array with language-tag-strings (must be lowercase) that are available
$http_accept_language a HTTP_ACCEPT_LANGUAGE string (read from $_SERVER['HTTP_ACCEPT_LANGUAGE'] if left out)
*/
function prefered_language ($available_languages,$http_accept_language="auto") {
// if $http_accept_language was left out, read it from the HTTP-Header
if ($http_accept_language == "auto") $http_accept_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
// standard for HTTP_ACCEPT_LANGUAGE is defined under
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
// pattern to find is therefore something like this:
// 1#( language-range [ ";" "q" "=" qvalue ] )
// where:
// language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
// qvalue = ( "0" [ "." 0*3DIGIT ] )
// | ( "1" [ "." 0*3("0") ] )
preg_match_all("/([[:alpha:]]{1,8})(-([[:alpha:]|-]{1,8}))?" .
"(\s*;\s*q\s*=\s*(1\.0{0,3}|0\.\d{0,3}))?\s*(,|$)/i",
$http_accept_language, $hits, PREG_SET_ORDER);
// default language (in case of no hits) is the first in the array
$bestlang = $available_languages[0];
$bestqval = 0;
foreach ($hits as $arr) {
// read data from the array of this hit
$langprefix = strtolower ($arr[1]);
if (!empty($arr[3])) {
$langrange = strtolower ($arr[3]);
$language = $langprefix . "-" . $langrange;
}
else $language = $langprefix;
$qvalue = 1.0;
if (!empty($arr[5])) $qvalue = floatval($arr[5]);
// find q-maximal language
if (in_array($language,$available_languages) && ($qvalue > $bestqval)) {
$bestlang = $language;
$bestqval = $qvalue;
}
// if no direct hit, try the prefix only but decrease q-value by 10% (as http_negotiate_language does)
else if (in_array($langprefix,$available_languages) && (($qvalue*0.9) > $bestqval)) {
$bestlang = $langprefix;
$bestqval = $qvalue*0.9;
}
}
return $bestlang;
}
?>