Skip to content
Snippets Groups Projects
Commit 2061fd3a authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Improved locale detection

parent 125a6d19
No related branches found
No related tags found
No related merge requests found
......@@ -8,21 +8,10 @@ require 'DatabaseAuthenticator.php';
* Set locale based on browser language specification, and set up gettext
*/
$supported_locales = array('nl_NL'); // English is default, Dutch is optional
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$locale = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE'])[0];
// 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);
}
if ($locale = set_accept_locale(array('nl'))) {
putenv("LANG=$locale");
putenv("LC_ALL=$locale");
putenv("LC_MESSAGES=$locale");
}
$domain = 'archery';
......
......@@ -15,3 +15,30 @@ function load_config($filename, $optional=false) {
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;
}
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