diff options
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | langs.inc.php | 22 | ||||
-rw-r--r-- | lib/t/03_lang_redirection.t | 13 | ||||
-rw-r--r-- | localeDetection.class.php | 8 |
4 files changed, 32 insertions, 12 deletions
@@ -28,6 +28,7 @@ TODO * Permissions in var/tmp/cache/ (rw) * short_open_tags = false * error_log directives +* rewrite language tags (and URLs) in "zh-CN" format (and not "zh-cn") Translators diff --git a/langs.inc.php b/langs.inc.php index d9e1d6ecc..e39afa3f9 100644 --- a/langs.inc.php +++ b/langs.inc.php @@ -88,26 +88,30 @@ function domain_redirect($host, $domains_lang, $vhost) /** * Redirect to a localized path, after browser Accept-Language prefs. + * Return the path. + * Do not exit the process. * * @param array $langs list of languages * @param string $page optional path to which we want to redirect * @param string $default_locale + * @param string $force_accept_language replace remote browser HTTP_ACCEPT_LANGUAGE request header * - * @return void + * @return string */ -function relocate($langs, $page = '', $default_locale = 'en') +function relocate($langs, $page = '', $default_locale = 'en', $force_accept_language = null) { require_once 'localeDetection.class.php'; - $locale = new ChooseLocale(array_keys($langs)); - + $locale = new ChooseLocale(array_keys($langs), $force_accept_language); $locale->setDefaultLocale($default_locale); - header(str_replace('//', '/', sprintf('Location: /%s/%s', - $locale->getCompatibleLocale(), - $page - ))); - die; + $relocate = sprintf('/%s/%s', $locale->getCompatibleLocale(), $page); + $relocate = str_replace('//', '/', $relocate); + + if ('cli' != PHP_SAPI) { + header('Location: ' . $relocate); + } + return $relocate; } /** diff --git a/lib/t/03_lang_redirection.t b/lib/t/03_lang_redirection.t new file mode 100644 index 000000000..cb799c71b --- /dev/null +++ b/lib/t/03_lang_redirection.t @@ -0,0 +1,13 @@ +<?php + +require realpath(__DIR__ . '/../testmore.php'); +require realpath(__DIR__ . '/../../langs.inc.php'); + +plan('no_plan'); + +diag('Testing langs.inc.php functions.'); + + +is(relocate($langs, null, 'en', 'en'), '/en/'); +is(relocate($langs, null, 'en', 'pt-BR,pt;q=0.8,es;q=0.6,en-US;q=0.4,en;q=0.2'), '/pt-br/'); + diff --git a/localeDetection.class.php b/localeDetection.class.php index 40e9862e4..d1b20357d 100644 --- a/localeDetection.class.php +++ b/localeDetection.class.php @@ -25,9 +25,11 @@ class ChooseLocale public $mapLonglocales; - public function __construct($list=array('en-US')) + public function __construct($list=array('en-US'), $force_http_accept_language = null) { - $this -> HTTPAcceptLang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; + $this -> HTTPAcceptLang = is_null($force_http_accept_language) ? + $_SERVER['HTTP_ACCEPT_LANGUAGE'] : + $force_http_accept_language; $this -> supportedLocales = array_unique($list); $this -> setDefaultLocale('en-US'); $this -> setCompatibleLocale(); @@ -39,7 +41,7 @@ class ChooseLocale { if (empty($this->HTTPAcceptLang)) return null; - return explode(',', $this->HTTPAcceptLang); + return explode(',', strtolower($this->HTTPAcceptLang)); } public function getCompatibleLocale() |