I create a function, which should return the best translation depending of user's locale.
The input is an array (from the BDD) of translation of the same text in many languages, the language is stored as the key like following:
$textes = array(2) {
["fr-fr"]=> string(24) "Editer l'item"
["en-en"]=> string(22) "Edit the item"
}
The function returns the item when $textes
contains only one item, else it should returns the best translation; I try to deal with intl's Locale function but it always return an empty string:
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];
}
So the lookup
function doesn't help me because it is not able to make a decision. Do I use it wrong? Or maybe I do not understand php-intl
's function's aim, and I should code it my myself?