diff options
author | Romain d'Alverny <rda@mageia.org> | 2010-10-22 08:15:12 +0000 |
---|---|---|
committer | Romain d'Alverny <rda@mageia.org> | 2010-10-22 08:15:12 +0000 |
commit | 5e711ba3b272ced1fee9dc0447f6402f377e7b23 (patch) | |
tree | ea69f3e72ff23a9662a2ad6f10b3aaa0699d7775 | |
parent | 40c0258381108d04f4e2a91713724849ad78ad3c (diff) | |
download | www-5e711ba3b272ced1fee9dc0447f6402f377e7b23.tar www-5e711ba3b272ced1fee9dc0447f6402f377e7b23.tar.gz www-5e711ba3b272ced1fee9dc0447f6402f377e7b23.tar.bz2 www-5e711ba3b272ced1fee9dc0447f6402f377e7b23.tar.xz www-5e711ba3b272ced1fee9dc0447f6402f377e7b23.zip |
redirect root access after browser lang prefs
-rw-r--r-- | langs.inc.php | 23 | ||||
-rw-r--r-- | localeDetection.class.php | 114 |
2 files changed, 137 insertions, 0 deletions
diff --git a/langs.inc.php b/langs.inc.php new file mode 100644 index 000000000..2d5c8b731 --- /dev/null +++ b/langs.inc.php @@ -0,0 +1,23 @@ +<?php + +// languages for home +$langs = array( + 'de' => 'Deutsch', + 'el' => 'Ελληνικά', + 'en' => 'English', + 'es' => 'Español', + 'et' => 'Eesti', + 'fi' => 'Suomeksi', + 'fr' => 'Français', + 'it' => 'Italiano', + 'lv' => 'Latviešu', + 'nb' => 'Bokmål', + 'nl' => 'Nederlands', + 'pl' => 'Polski', + 'pt-br' => 'Português do Brasil', + 'ru' => 'Русский', + 'tr' => 'Türkçe', + 'sl' => 'Slovenščina', + 'zh-cn' => '简体中文', + 'zh-tw' => '正體中文' +);
\ No newline at end of file diff --git a/localeDetection.class.php b/localeDetection.class.php new file mode 100644 index 000000000..40e9862e4 --- /dev/null +++ b/localeDetection.class.php @@ -0,0 +1,114 @@ +<?php + +/* ChooseLocale + * + * Licence: MPL 2/GPL 2.0/LGPL 2.1 + * Author: Pascal Chevrel, Mozilla + * Date : 2010-07-17 + * + * Description: + * Class to choose the locale which locale we will show to the visitor + * based on http acceptlang headers and our list of supported locales. + * + * +*/ + + + + +class ChooseLocale +{ + public $HTTPAcceptLang; + public $supportedLocales; + protected $detectedLocale; + protected $defaultLocale; + public $mapLonglocales; + + + public function __construct($list=array('en-US')) + { + $this -> HTTPAcceptLang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; + $this -> supportedLocales = array_unique($list); + $this -> setDefaultLocale('en-US'); + $this -> setCompatibleLocale(); + $this -> mapLonglocales = true; + + } + + public function getAcceptLangArray() + { + if (empty($this->HTTPAcceptLang)) return null; + + return explode(',', $this->HTTPAcceptLang); + } + + public function getCompatibleLocale() + { + $l = $this -> defaultLocale; + $acclang = $this -> getAcceptLangArray(); + + if(!is_array($acclang)) { + return $this -> defaultLocale; + } + + foreach ($acclang as $var) { + $locale = $this -> _cleanHTTPlocaleCode($var); + $shortLocale = array_shift(explode('-', $locale)); + + if (in_array($locale, $this -> supportedLocales)) { + $l = $locale; + break; + } + + if (in_array($shortLocale, $this -> supportedLocales)) { + $l = $shortLocale; + break; + } + + // check if we map visitors short locales to site long locales + // like en -> en-GB + if ($this -> mapLonglocales == true) { + foreach ($this -> supportedLocales as $var) { + $shortSupportedLocale = array_shift(explode('-', $var)); + if ($shortLocale == $shortSupportedLocale) { + $l = $var; + break; + } + } + } + + } + + return $l; + } + + public function getDefaultLocale() { + return $this -> defaultLocale; + } + + public function setCompatibleLocale() { + $this -> detectedLocale = $this -> getCompatibleLocale(); + } + + public function setDefaultLocale($locale) { + + // the default locale should always be among the site locales + // if not, the first locale in the supportedLocales array is default + if (!in_array($locale, $this -> supportedLocales)) { + $this -> defaultLocale = $this -> supportedLocales[0]; + + } else { + $this -> defaultLocale = $locale; + } + return; + } + + private function _cleanHTTPlocaleCode($str) + { + $locale = explode(';', $str); + $locale = trim($locale[0]); + + return $locale; + } + +}
\ No newline at end of file |