From 9ce6d646ff718cd62f6434c9a4c94e450436612a Mon Sep 17 00:00:00 2001 From: Thomas Backlund Date: Sun, 12 Oct 2014 12:27:45 +0300 Subject: Revert "Updated Estonian translation" This reverts commit a0b2d09125d7911656fe4cb286f20643c4432281. --- localeDetection.class.php | 119 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 localeDetection.class.php (limited to 'localeDetection.class.php') diff --git a/localeDetection.class.php b/localeDetection.class.php new file mode 100644 index 000000000..d2a3a089d --- /dev/null +++ b/localeDetection.class.php @@ -0,0 +1,119 @@ + HTTPAcceptLang = $force_http_accept_language; + } else if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + $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(',', strtolower($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 -- cgit v1.2.1