Index: branches/5.1.x/core/units/languages/languages_item.php =================================================================== diff -u -N -r13168 -r13489 --- branches/5.1.x/core/units/languages/languages_item.php (.../languages_item.php) (revision 13168) +++ branches/5.1.x/core/units/languages/languages_item.php (.../languages_item.php) (revision 13489) @@ -1,6 +1,6 @@ GetDBField('DecimalPoint'), $this->GetDBField('ThousandSep')); } + /** + * Returns language id based on HTTP header "Accept-Language" + * + * @return int + */ + function processAcceptLanguage() + { + if (!array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) || !$_SERVER['HTTP_ACCEPT_LANGUAGE']) { + return false; + } + + $accepted_languages = Array (); + $language_string = explode(',', strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE'])); + + foreach ($language_string as $mixed_language) { + if (strpos($mixed_language, ';') !== false) { + list ($language_locale, $language_quality) = explode(';', $mixed_language); + $language_quality = (float)substr($language_quality, 2); // format "q=4.45" + } + else { + $language_locale = $mixed_language; + $language_quality = 1.00; + } + + $accepted_languages[ trim($language_locale) ] = trim($language_quality); + } + + arsort($accepted_languages, SORT_NUMERIC); + + foreach ($accepted_languages as $language_locale => $language_quality) { + $language_id = $this->getAvailableLanguage($language_locale); + + if ($language_id) { + return $language_id; + } + } + + return false; + } + + /** + * Returns language ID based on given locale + * + * @param string $locale + * @return int + */ + function getAvailableLanguage($locale) + { + $cache_key = 'available_languages[%LangSerial%]'; + $available_languages = $this->Application->getCache($cache_key); + + if ($available_languages === false) { + $this->Conn->nextQueryCachable = true; + $sql = 'SELECT LanguageId, LOWER(Locale) AS Locale + FROM ' . TABLE_PREFIX . 'Language + WHERE Enabled = 1'; + $available_languages = $this->Conn->GetCol($sql, 'Locale'); + $this->Application->setCache($cache_key, $available_languages); + } + + if (strpos($locale, '-') !== false) { + // exact language match requested + return array_key_exists($locale, $available_languages) ? $available_languages[$locale] : false; + } + + // partial (like "en" matches "en-GB" and "en-US") language match required + foreach ($available_languages as $language_code => $language_id) { + list ($language_code, ) = explode('-', $language_code); + + if ($locale == $language_code) { + return $language_id; + } + } + + return false; + } + + /** + * Checks, that url is empty + * + * @return bool + */ + function isEmptyUrl() + { + if ($this->Application->RewriteURLs()) { + return !$this->Application->GetVar('_mod_rw_url_'); + } + + return !count($this->Application->HttpQuery->Get); + } + function Load($id, $id_field_name=null) { $default = false; if ($id == 'default') { - $id = 1; - $id_field_name = 'PrimaryLang'; $default = true; - } + $res = parent::Load(1, 'PrimaryLang', true); - $res = parent::Load($id, $id_field_name, true); + if ( + !$this->Application->isAdmin && $this->isEmptyUrl() && + $this->Application->ConfigValue('UseContentLanguageNegotiation') + ) { + $language_id = $this->processAcceptLanguage(); + if ($language_id != $this->GetID()) { + // redirect to same page with found language + $url_params = Array ( + 'm_cat_id' => 0, 'm_cat_page' => 1, + 'm_lang' => $language_id, 'm_opener' => 's', + 'pass' => 'm' + ); + + $this->Application->Redirect('', $url_params); + } + } + } + else { + $res = parent::Load($id, $id_field_name, true); + } + if ($default) { if (!$res) { if ($this->Application->isAdmin) { @@ -86,6 +195,7 @@ $this->Application->SetVar('lang.current_id', $this->GetID() ); $this->Application->SetVar('m_lang', $this->GetID() ); } + return $res; } } \ No newline at end of file