Skip to content
Snippets Groups Projects
Commit f2b0c8db authored by Olav Morken's avatar Olav Morken
Browse files

Changed template code to use languages from the Accept-Language http header.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@477 44740490-163a-0410-bde0-09ae8108e29a
parent 0c88d274
No related branches found
No related tags found
No related merge requests found
......@@ -46,21 +46,55 @@ class SimpleSAML_XHTML_Template {
// Language is set in object
if (isset($this->language)) {
return $this->language;
}
// Language is provided in a stored COOKIE
} else if (isset($_COOKIE['language'])) {
if (isset($_COOKIE['language'])) {
$this->language = $_COOKIE['language'];
return $this->language;
}
/* Check if we can find a good language from the Accept-Language http header. */
$httpLanguage = $this->getHTTPLanguage();
if ($httpLanguage !== NULL) {
return $httpLanguage;
}
// Language is not set, and we get the default language from the configuration.
} else {
#$nego = http_negotiate_language($this->configuration->getValue('language.available'));
return $this->getDefaultLanguage();
return $this->getDefaultLanguage();
}
/**
* This function gets the prefered language for the user based on the Accept-Language http header.
*
* @return The prefered language based on the Accept-Language http header, or NULL if none of the
* languages in the header were available.
*/
private function getHTTPLanguage() {
$availableLanguages = $this->configuration->getValue('language.available');
$languageScore = SimpleSAML_Utilities::getAcceptLanguage();
/* Find the available language with the best score. */
$bestLanguage = NULL;
$bestScore = -1.0;
foreach($availableLanguages as $language) {
if(!array_key_exists($language, $languageScore)) {
/* Skip this language - it wasn't mentioned in the http header. */
continue;
}
$score = $languageScore[$language];
if($score > $bestScore) {
$bestLanguage = $language;
$bestScore = $score;
}
}
return $this->language;
return $bestLanguage;
}
private function getDefaultLanguage() {
return $this->configuration->getValue('language.default', 'en');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment