mirror of
https://github.com/LBRYFoundation/lbry.com.git
synced 2025-09-04 21:05:15 +00:00
Merge branch 'i18n'
* i18n: hide lang switcher for now fixes minor i18n tweaks i18n: Improve language detection i18n: Implement getAllCultures i18n: Send a POST request instead of an AJAX request i18n: Add some comments Nav: Add a (temporary) dropdown to allow user to change language i18n: Store user culture in session
This commit is contained in:
commit
26d58a9b36
8 changed files with 127 additions and 25 deletions
|
@ -94,6 +94,8 @@ class Controller
|
|||
$router->any('/list/subscribe', 'MailActions::executeSubscribe');
|
||||
$router->get('/list/confirm/{hash}', 'MailActions::executeConfirm');
|
||||
|
||||
$router->post('/set-culture', 'i18nActions::setCulture');
|
||||
|
||||
$permanentRedirects = [
|
||||
'/lbry-osx-latest.dmg' => '/get',
|
||||
'/lbry-linux-latest.deb' => '/get',
|
||||
|
|
|
@ -6,7 +6,8 @@ class Session
|
|||
KEY_DOWNLOAD_ALLOWED = 'beta_download_allowed2',
|
||||
KEY_PREFINERY_USER_ID = 'prefinery_user_id',
|
||||
KEY_PREFINER_USED_CUSTOM_CODE = 'prefinery_used_custom_code',
|
||||
KEY_LIST_SUB_ERROR = 'list_error';
|
||||
KEY_LIST_SUB_ERROR = 'list_error',
|
||||
KEY_USER_CULTURE = 'user_culture';
|
||||
|
||||
public static function init()
|
||||
{
|
||||
|
|
|
@ -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)
|
||||
|
|
29
controller/action/i18nActions.class.php
Normal file
29
controller/action/i18nActions.class.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
class i18nActions extends Actions
|
||||
{
|
||||
public static function setCulture()
|
||||
{
|
||||
$culture = Request::getPostParam('culture');
|
||||
|
||||
// Validate
|
||||
if ($culture && !in_array($culture, i18n::getAllCultures()))
|
||||
{
|
||||
$culture = null;
|
||||
}
|
||||
|
||||
if ($culture)
|
||||
{
|
||||
Session::set(Session::KEY_USER_CULTURE, $culture);
|
||||
}
|
||||
else
|
||||
{
|
||||
Session::unsetKey(Session::KEY_USER_CULTURE);
|
||||
}
|
||||
|
||||
//if session changes update domain
|
||||
//english language = www
|
||||
|
||||
return Controller::redirect(Request::getHttpHeader('Referer', '/'));
|
||||
}
|
||||
}
|
|
@ -13,28 +13,16 @@ class i18n
|
|||
protected static
|
||||
$language = null,
|
||||
$translations = [],
|
||||
$country = null;
|
||||
$country = null,
|
||||
$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*/
|
||||
{
|
||||
if ($culture === null)
|
||||
{
|
||||
$urlTokens = Request::getHost() ? explode('.', Request::getHost()) : [];
|
||||
$code = $urlTokens ? reset($urlTokens) : 'en';
|
||||
switch($code)
|
||||
{
|
||||
case 'pt':
|
||||
$culture = 'pt_PT'; break;
|
||||
case 'en':
|
||||
case 'www':
|
||||
default:
|
||||
$culture = 'en_US';
|
||||
}
|
||||
}
|
||||
$culture = static::deduceCulture();
|
||||
|
||||
list($language, $country) = explode('_', $culture);
|
||||
static::$language = $language;
|
||||
static::$country = $country;
|
||||
static::$country = $country;
|
||||
|
||||
setlocale(LC_MONETARY, $culture . '.UTF-8');
|
||||
}
|
||||
|
@ -49,6 +37,11 @@ class i18n
|
|||
return static::$country;
|
||||
}
|
||||
|
||||
public static function getAllCultures()
|
||||
{
|
||||
return static::$cultures;
|
||||
}
|
||||
|
||||
public static function formatCurrency($amount, $currency = 'USD')
|
||||
{
|
||||
return '<span class="formatted-currency">' . money_format('%.2n', $amount) . '</span>';
|
||||
|
@ -56,7 +49,7 @@ class i18n
|
|||
|
||||
public static function formatCredits($amount)
|
||||
{
|
||||
return '<span class="formatted-credits">' . (is_numeric($amount) ? number_format($amount, 1) : $amount) . ' LBC</span>';
|
||||
return '<span class="formatted-credits">' . (is_numeric($amount) ? number_format($amount, 1) : $amount) . ' LBC</span>';
|
||||
}
|
||||
|
||||
public static function translate($token, $language = null)
|
||||
|
@ -65,10 +58,11 @@ class i18n
|
|||
if (!isset(static::$translations[$language]))
|
||||
{
|
||||
$path = ROOT_DIR . '/data/i18n/' . $language . '.yaml';
|
||||
|
||||
static::$translations[$language] = file_exists($path) ? Spyc::YAMLLoadString(file_get_contents($path)) : [];
|
||||
}
|
||||
$scope = static::$translations[$language];
|
||||
foreach(explode('.', $token) as $level)
|
||||
foreach (explode('.', $token) as $level)
|
||||
{
|
||||
if (isset($scope[$level]))
|
||||
{
|
||||
|
@ -85,4 +79,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 (Request::getHttpHeader('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', Request::getHttpHeader('Accept-Language'), $languages);
|
||||
|
||||
if (isset($languages[1]) && count($languages[1]))
|
||||
{
|
||||
// create a list like "en" => 0.8
|
||||
$langs = array_combine($languages[1], $languages[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';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,4 +21,17 @@
|
|||
</div>
|
||||
<div class="control-item no-label-desktop">
|
||||
<a href="https://github.com/lbryio"><span class="btn-label">GitHub</span><span class="icon-github icon-fw"></span></a>
|
||||
</div>
|
||||
</div>
|
||||
<?php /*
|
||||
<div class="control-item no-label-desktop">
|
||||
<form action="/set-culture" method="POST">
|
||||
<select id="language-dropdown" name="culture">
|
||||
<?php foreach ($cultures as $culture): ?>
|
||||
<option value="<?php echo $culture ?>" <?php echo $culture == $selectedCulture ? 'selected="selected"' : '' ?>>
|
||||
<?php echo $culture ?>
|
||||
</option>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
</form>
|
||||
</div>
|
||||
*/ ?>
|
|
@ -25,8 +25,8 @@ if (!IS_PRODUCTION)
|
|||
|
||||
try
|
||||
{
|
||||
i18n::register();
|
||||
Session::init();
|
||||
i18n::register();
|
||||
if (!IS_PRODUCTION)
|
||||
{
|
||||
View::compileCss();
|
||||
|
|
|
@ -162,4 +162,8 @@ $(document).ready(function() {
|
|||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
$('#language-dropdown').on('change', function() {
|
||||
$(this).closest('form').submit();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue