Skip to content

Commit

Permalink
// Fix PSCFV-10876: use IETF code to set language of shop, not 'iso' …
Browse files Browse the repository at this point in the history
…code.
  • Loading branch information
djfm committed Nov 4, 2013
1 parent 7812731 commit 8e4faaa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
29 changes: 29 additions & 0 deletions classes/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,35 @@ public static function getLanguageCodeByIso($iso_code)
return Db::getInstance()->getValue('SELECT `language_code` FROM `'._DB_PREFIX_.'lang` WHERE `iso_code` = \''.pSQL(strtolower($iso_code)).'\'');
}

public static function getLanguageByIETFCode($code)
{
if (!Validate::isLanguageCode($code))
die(sprintf(Tools::displayError('Fatal error: IETF code %s is not correct'), $code));

// $code is in the form of 'xx-YY' where xx is the language code
// and 'YY' a country code identifying a variant of the language.
$lang_country = explode('-', $code);
// Get the language component of the code
$lang = $lang_country[0];

// Find the id_lang of the language.
// We look for anything with the correct language code
// and sort on equality with the exact IETF code wanted.
// That way using only one query we get either the exact wanted language
// or a close match.
$id_lang = Db::getInstance()->getValue(
'SELECT `id_lang` FROM '
.'`'._DB_PREFIX_.'lang` WHERE LEFT(`language_code`,2) = \''.pSQL(strtolower($lang)).'\' '
.'ORDER BY language_code = \''.pSQL($code).'\' DESC'
);

// Instantiate the Language object if we found it.
if ($id_lang)
return new Language($id_lang);
else return false;

}

/**
* Return array (id_lang, iso_code)
*
Expand Down
18 changes: 7 additions & 11 deletions classes/Tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,21 @@ public static function setCookieLanguage($cookie = null)
/* Automatically detect language if not already defined, detect_language is set in Cookie::update */
if ((!$cookie->id_lang || isset($cookie->detect_language)) && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
$array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']));
if (Tools::strlen($array[0]) > 2)
{
$tab = explode('-', $array[0]);
$string = $tab[0];
}
else
$string = $array[0];
if (Validate::isLanguageIsoCode($string))

$array = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$string = $array[0];

if (Validate::isLanguageCode($string))
{
$lang = new Language(Language::getIdByIso($string));
$lang = Language::getLanguageByIETFCode($string);
if (Validate::isLoadedObject($lang) && $lang->active && $lang->isAssociatedToShop())
{
Context::getContext()->language = $lang;
$cookie->id_lang = (int)$lang->id;
}
}
}

if (isset($cookie->detect_language))
unset($cookie->detect_language);

Expand Down

0 comments on commit 8e4faaa

Please sign in to comment.