From 743c4f4607bfc3f2fa06cca889face8f4549b311 Mon Sep 17 00:00:00 2001 From: fram Date: Tue, 12 Nov 2013 14:59:36 +0100 Subject: [PATCH] [*] LO : Fix PSCFV-10876: use IETF code to set language of shop, not 'iso' code, allows to distinguish between chinese variants etc. --- classes/Language.php | 29 +++++++++++++++++++++++++++++ classes/Tools.php | 19 +++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/classes/Language.php b/classes/Language.php index ce3762ab2..ffa4b12e9 100644 --- a/classes/Language.php +++ b/classes/Language.php @@ -619,6 +619,35 @@ class LanguageCore extends ObjectModel 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($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) * diff --git a/classes/Tools.php b/classes/Tools.php index f9f6eddd7..801d9bf4b 100644 --- a/classes/Tools.php +++ b/classes/Tools.php @@ -346,17 +346,12 @@ class ToolsCore /* 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) + $array = explode(',', Tools::strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])); + $string = $array[0]; + + if (Validate::isLanguageCode($string)) { - $tab = explode('-', $array[0]); - $string = $tab[0]; - } - else - $string = $array[0]; - if (Validate::isLanguageIsoCode($string)) - { - $lang = new Language(Language::getIdByIso($string)); + $lang = Language::getLanguageByIETFCode($string); if (Validate::isLoadedObject($lang) && $lang->active && $lang->isAssociatedToShop()) { Context::getContext()->language = $lang; @@ -364,7 +359,7 @@ class ToolsCore } } } - + if (isset($cookie->detect_language)) unset($cookie->detect_language); @@ -374,7 +369,7 @@ class ToolsCore $iso = Language::getIsoById((int)$cookie->id_lang); @include_once(_PS_THEME_DIR_.'lang/'.$iso.'.php'); - + return $iso; }