i18n tweaks

This commit is contained in:
Jeremy Kauffman 2016-09-05 11:32:47 -04:00 committed by Alex Grintsvayg
parent 982ffa1f51
commit 1aeb5618bb
5 changed files with 76 additions and 58 deletions

View file

@ -23,8 +23,11 @@ class NavActions extends Actions
public static function prepareGlobalItemsPartial(array $vars) 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) public static function execute400(array $vars)

View file

@ -28,6 +28,9 @@ class i18nActions extends Actions
Session::unsetKey(Session::KEY_USER_CULTURE); Session::unsetKey(Session::KEY_USER_CULTURE);
} }
return Controller::redirect('/'); //if session changes update domain
//english language = www
return Controller::redirect($_SERVER['HTTP_REFERER'] ?: '/');
} }
} }

View file

@ -14,46 +14,11 @@ class i18n
$language = null, $language = null,
$translations = [], $translations = [],
$country = null, $country = null,
$cultures = ['pt_PT', 'en_US'], $cultures = ['pt_PT', 'en_US'];
$subdomainToCulture = array(
'pt' => 'pt_PT',
'en' => '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 $culture = static::deduceCulture();
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';
}
list($language, $country) = explode('_', $culture); list($language, $country) = explode('_', $culture);
static::$language = $language; static::$language = $language;
@ -113,4 +78,60 @@ class i18n
} }
return $scope ?: $token; 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';
}
} }

View file

@ -23,18 +23,11 @@
<a href="https://github.com/lbryio"><span class="btn-label">GitHub</span><span class="icon-github icon-fw"></span></a> <a href="https://github.com/lbryio"><span class="btn-label">GitHub</span><span class="icon-github icon-fw"></span></a>
</div> </div>
<div class="control-item no-label-desktop"> <div class="control-item no-label-desktop">
<form id="language-form" action="/set-culture" method="POST"> <form action="/set-culture" method="POST">
<select id="language-dropdown" name="culture"> <select id="language-dropdown" name="culture">
<?php <?php foreach ($cultures as $culture): ?>
foreach (i18n::getAllCultures() as $culture) <option <?php echo $culture == $selectedCulture ? 'selected="selected"' : '' ?>><?php echo $culture ?></option>
{ <?php endforeach ?>
echo "<option>$culture</option>";
}
?>
</select> </select>
</form> </form>
</div> </div>
<script type="text/javascript">
var _currentLang = '<?php echo i18n::getLanguage()."_".i18n::getCountry() ?>';
</script>

View file

@ -163,9 +163,7 @@ $(document).ready(function() {
}); });
} }
var langDropdown = $('#language-dropdown'); $('#language-dropdown').on('change', function() {
langDropdown.val(_currentLang); $(this).closest('form').submit();
langDropdown.on('change', function(x) {
$('#language-form').submit();
}); });
}); });