mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-08-23 17:47:26 +00:00
i18n tweaks
This commit is contained in:
parent
982ffa1f51
commit
1aeb5618bb
5 changed files with 76 additions and 58 deletions
|
@ -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)
|
||||
|
|
|
@ -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'] ?: '/');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,18 +23,11 @@
|
|||
<a href="https://github.com/lbryio"><span class="btn-label">GitHub</span><span class="icon-github icon-fw"></span></a>
|
||||
</div>
|
||||
<div class="control-item no-label-desktop">
|
||||
<form id="language-form" action="/set-culture" method="POST">
|
||||
<select id="language-dropdown" name="culture">
|
||||
<?php
|
||||
foreach (i18n::getAllCultures() as $culture)
|
||||
{
|
||||
echo "<option>$culture</option>";
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<form action="/set-culture" method="POST">
|
||||
<select id="language-dropdown" name="culture">
|
||||
<?php foreach ($cultures as $culture): ?>
|
||||
<option <?php echo $culture == $selectedCulture ? 'selected="selected"' : '' ?>><?php echo $culture ?></option>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script type="text/javascript">
|
||||
var _currentLang = '<?php echo i18n::getLanguage()."_".i18n::getCountry() ?>';
|
||||
</script>
|
||||
|
|
|
@ -163,9 +163,7 @@ $(document).ready(function() {
|
|||
});
|
||||
}
|
||||
|
||||
var langDropdown = $('#language-dropdown');
|
||||
langDropdown.val(_currentLang);
|
||||
langDropdown.on('change', function(x) {
|
||||
$('#language-form').submit();
|
||||
$('#language-dropdown').on('change', function() {
|
||||
$(this).closest('form').submit();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue