Commit 2061fd3a authored by Taddeus Kroes's avatar Taddeus Kroes

Improved locale detection

parent 125a6d19
...@@ -8,21 +8,10 @@ require 'DatabaseAuthenticator.php'; ...@@ -8,21 +8,10 @@ require 'DatabaseAuthenticator.php';
* Set locale based on browser language specification, and set up gettext * Set locale based on browser language specification, and set up gettext
*/ */
$supported_locales = array('nl_NL'); // English is default, Dutch is optional if ($locale = set_accept_locale(array('nl'))) {
putenv("LANG=$locale");
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { putenv("LC_ALL=$locale");
$locale = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0]; putenv("LC_MESSAGES=$locale");
// nl -> nl_NL
if (preg_match('/^[a-z]{2}$/', $locale))
$locale .= '_' . strtoupper($locale);
if (in_array($locale, $supported_locales)) {
putenv("LANG=$locale");
putenv("LC_ALL=$locale");
putenv("LC_MESSAGES=$locale");
setlocale(LC_ALL, $locale);
}
} }
$domain = 'archery'; $domain = 'archery';
......
...@@ -15,3 +15,30 @@ function load_config($filename, $optional=false) { ...@@ -15,3 +15,30 @@ function load_config($filename, $optional=false) {
return $config; return $config;
} }
function set_accept_locale($supported_languages=null) {
if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
return null;
foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $locale) {
if (!preg_match('/^(\w\w)(?:[-_](\w\w))?(\.\w+)?$/', $locale, $m))
continue;
$lang = $m[1];
$region = isset($m[2]) ? '_'.$m[2] : '';
$ext = isset($m[3]) ? $m[3] : '';
if ($supported_languages === null || in_array($lang, $supported_languages)) {
$locale = setlocale(LC_ALL,
$lang.$region.$ext,
$lang.$region.'.utf8',
$lang.$region,
$lang.$ext,
$lang.'.utf8',
$lang);
return $locale;
}
}
return null;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment