diff --git a/controller/action/NavActions.class.php b/controller/action/NavActions.class.php index 4c10a227..8f91f5d1 100644 --- a/controller/action/NavActions.class.php +++ b/controller/action/NavActions.class.php @@ -23,8 +23,11 @@ class NavActions extends Actions public static function prepareGlobalItemsPartial(array $vars) { - $vars += ['selectedItem' => static::getNavUri()]; - return $vars; + return $vars += [ + 'selectedItem' => static::getNavUri(), + 'selectedCulture' => i18n::getLanguage() . '_' . i18n::getCountry(), + 'cultures' => i18n::getAllCultures() + ]; } public static function execute400(array $vars) diff --git a/controller/action/i18nActions.class.php b/controller/action/i18nActions.class.php index 0310d050..f56693b6 100644 --- a/controller/action/i18nActions.class.php +++ b/controller/action/i18nActions.class.php @@ -28,6 +28,9 @@ class i18nActions extends Actions Session::unsetKey(Session::KEY_USER_CULTURE); } - return Controller::redirect('/'); + //if session changes update domain + //english language = www + + return Controller::redirect($_SERVER['HTTP_REFERER'] ?: '/'); } } diff --git a/lib/i18n.class.php b/lib/i18n.class.php index bc8d5ebe..b02aaa52 100644 --- a/lib/i18n.class.php +++ b/lib/i18n.class.php @@ -14,46 +14,11 @@ class i18n $language = null, $translations = [], $country = null, - $cultures = ['pt_PT', 'en_US'], - $subdomainToCulture = array( - 'pt' => 'pt_PT', - 'en' => 'en_US' - ); + $cultures = ['pt_PT', 'en_US']; - public static function register($culture = null) /*needed to trigger class include, presumably setup would happen here*/ + public static function register() /*needed to trigger class include, presumably setup would happen here*/ { - // Get user preference, if any - if ($culture === null) - { - $culture = Session::get(Session::KEY_USER_CULTURE); - } - - // Deduce from subdomain - if ($culture === null) - { - $urlTokens = Request::getHost() ? explode('.', Request::getHost()) : []; - $code = $urlTokens ? reset($urlTokens) : 'en'; - if (in_array($code, static::$subdomainToCulture)) - { - $culture = static::$subdomainToCulture[$code]; - } - } - - // Deduce from HTTP_ACCEPT_LANGUAGE - if ($culture === null) - { - $code = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); - if (in_array($code, static::$cultures)) - { - $culture = $code; - } - } - - // Default to en_US - if ($culture === null) - { - $culture = 'en_US'; - } + $culture = static::deduceCulture(); list($language, $country) = explode('_', $culture); static::$language = $language; @@ -113,4 +78,60 @@ class i18n } return $scope ?: $token; } + + protected static function deduceCulture() + { + $candidates = []; + + //url trumps everything + $urlTokens = Request::getHost() ? explode('.', Request::getHost()) : []; + $code = $urlTokens ? reset($urlTokens) : null; + if ($code !== 'www') + { + $candidates[] = $code; + } + + //then session + $candidates[] = Session::get(Session::KEY_USER_CULTURE); + + // then headers + // http://www.thefutureoftheweb.com/blog/use-accept-language-header + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) + { + // break up string into pieces (languages and q factors) + preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); + + if (count($lang_parse[1])) + { + // create a list like "en" => 0.8 + $langs = array_combine($lang_parse[1], $lang_parse[4]); + + // set default to 1 for any without q factor + foreach ($langs as $lang => $val) + { + if ($val === '') + { + $langs[$lang] = 1; + } + } + + arsort($langs, SORT_NUMERIC); + + $candidates = array_merge($candidates, array_keys($langs)); + } + } + + foreach($candidates as $candidate) + { + foreach (static::getAllCultures() as $culture) + { + if ($candidate === $culture || substr($culture, 0, 2) === $candidate) + { + return $culture; + } + } + } + + return 'en_US'; + } } diff --git a/view/template/nav/_globalItems.php b/view/template/nav/_globalItems.php index d974fdf0..9e27de83 100644 --- a/view/template/nav/_globalItems.php +++ b/view/template/nav/_globalItems.php @@ -23,18 +23,11 @@ GitHub