// Set a default country/timezone in installer, checking HTTP_ACCEPT_LANGUAGE #PSTEST-890

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@13714 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2012-02-29 09:02:36 +00:00
parent bae15bcbcc
commit d97ecb2315
3 changed files with 45 additions and 35 deletions
+6 -20
View File
@@ -175,7 +175,12 @@ abstract class InstallControllerHttp
// Set current language
$this->language = InstallLanguages::getInstance();
$lang = (isset($this->session->lang)) ? $this->session->lang : $this->detectLanguage();
$detect_language = $this->language->detectLanguage();
if (isset($this->session->lang))
$lang = $this->session->lang;
else
$lang = (isset($detect_language['primarytag'])) ? $detect_language['primarytag'] : false;
if (!in_array($lang, $this->language->getIsoList()))
$lang = 'en';
$this->language->setLanguage($lang);
@@ -391,25 +396,6 @@ abstract class InstallControllerHttp
}
}
public function detectLanguage()
{
// This code is from a php.net comment : http://www.php.net/manual/fr/reserved.variables.server.php#94237
$split_languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!is_array($split_languages))
return false;
foreach ($split_languages as $lang)
{
$pattern = '/^(?P<primarytag>[a-zA-Z]{2,8})'.
'(?:-(?P<subtag>[a-zA-Z]{2,8}))?(?:(?:;q=)'.
'(?P<quantifier>\d\.\d))?$/';
if (preg_match($pattern, $lang, $m))
if (in_array($m['primarytag'], $this->language->getIsoList()))
return $m['primarytag'];
}
return false;
}
public function &__get($varname)
{
if (isset($this->__vars[$varname]))
+24
View File
@@ -178,4 +178,28 @@ class InstallLanguages
return $countries;
}
/**
* Parse HTTP_ACCEPT_LANGUAGE and get first data matching list of available languages
*
* @return bool|array
*/
public function detectLanguage()
{
// This code is from a php.net comment : http://www.php.net/manual/fr/reserved.variables.server.php#94237
$split_languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!is_array($split_languages))
return false;
foreach ($split_languages as $lang)
{
$pattern = '/^(?P<primarytag>[a-zA-Z]{2,8})'.
'(?:-(?P<subtag>[a-zA-Z]{2,8}))?(?:(?:;q=)'.
'(?P<quantifier>\d\.\d))?$/';
if (preg_match($pattern, $lang, $m))
if (in_array($m['primarytag'], $this->getIsoList()))
return $m;
}
return false;
}
}
+15 -15
View File
@@ -266,21 +266,11 @@ class InstallControllerHttpConfigure extends InstallControllerHttp
$this->list_countries = array();
$countries = $this->language->getCountries();
$top_countries = array(
'fr',
'es',
'us',
'gb',
'it',
'de',
'nl',
'pl',
'id',
'be',
'br',
'se',
'ca',
'ru',
'cn',
'fr', 'es', 'us',
'gb', 'it', 'de',
'nl', 'pl', 'id',
'be', 'br', 'se',
'ca', 'ru', 'cn',
);
foreach ($top_countries as $iso)
@@ -291,6 +281,16 @@ class InstallControllerHttpConfigure extends InstallControllerHttp
if (!in_array($iso, $top_countries))
$this->list_countries[] = array('iso' => $iso, 'name' => $lang);
// Try to detect default country
if (!$this->session->shop_country)
{
$detect_language = $this->language->detectLanguage();
if (isset($detect_language['primarytag']))
{
$this->session->shop_country = (isset($detect_language['subtag'])) ? $detect_language['subtag'] : $detect_language['primarytag'];
$this->session->shop_timezone = $this->getTimezoneByIso($this->session->shop_country);
}
}
// Install type
$this->install_type = ($this->session->install_type) ? $this->session->install_type : 'full';