Restore full local name in settings

- Fallback to two-letter language code for translation if locale isn't found
- Rename L10n::langToString to L10n::localeToLanguageString
- Use setlocale() with the selected locale
This commit is contained in:
Hypolite Petovan 2018-11-18 10:38:17 -05:00
parent 012a3f9f8d
commit e8752c7631
7 changed files with 75 additions and 11 deletions

View file

@ -10,6 +10,9 @@ use Gettext\Translator;
*/ */
class L10n class L10n
{ {
const DECIMAL = 1;
const PERCENT = 2;
public static $languages = [ public static $languages = [
'af' => 'Afrikaans', 'af' => 'Afrikaans',
'ak' => 'Akan', 'ak' => 'Akan',
@ -80,6 +83,7 @@ class L10n
'ks' => 'کٲشُر', 'ks' => 'کٲشُر',
'kw' => 'Kernewek', 'kw' => 'Kernewek',
'ky' => 'Кыргызча', 'ky' => 'Кыргызча',
'la' => 'Lingua Latina',
'lb' => 'Lëtzebuergesch', 'lb' => 'Lëtzebuergesch',
'lg' => 'Luganda', 'lg' => 'Luganda',
'ln' => 'Lingála', 'ln' => 'Lingála',
@ -150,16 +154,62 @@ class L10n
'zu' => 'Isizulu', 'zu' => 'Isizulu',
]; ];
public static function langToString($lang) public static function localeToLanguageString($locale)
{ {
$found = false; $lang = substr($locale, 0, 2);
$foundLocale = false;
$foundLang = false;
foreach(self::$languages as $key => $language) { foreach(self::$languages as $key => $language) {
if (strtolower($key) == strtolower(str_replace('-', '_', $lang))) { if (strtolower($key) == strtolower($lang)) {
$found = true; $foundLang = $language;
}
if (strtolower($key) == strtolower(str_replace('-', '_', $locale))) {
$foundLocale = true;
break; break;
} }
} }
return $found ? $language : $lang; return $foundLocale ? $language : $foundLang ?: $locale;
}
/**
* @param float|int $number
* @param int $style
* @return string
*/
public static function formatNumber($number, $style = self::DECIMAL)
{
$locale = localeconv();
switch($style) {
case self::PERCENT:
$number *= 100;
if (\intval($number) == $number) {
$decimals = 0;
} else {
$decimals = 2;
}
$return = number_format($number, $decimals,
$locale['decimal_point'],
$locale['thousands_sep']) . '%';
break;
case self::DECIMAL:
default:
if (\intval($number) == $number) {
$decimals = 0;
} else {
$decimals = 2;
}
$return = number_format($number, $decimals,
$locale['decimal_point'],
$locale['thousands_sep']);
break;
}
return $return;
} }
} }

View file

@ -14,6 +14,12 @@ $middleware = new LocalizationMiddleware(
); );
$middleware->setLocaleCallback(function (string $locale) use ($container) { $middleware->setLocaleCallback(function (string $locale) use ($container) {
setlocale(LC_ALL, $locale);
setlocale(LC_ALL, $locale . '.utf8');
setlocale(LC_ALL, $locale . '.UTF8');
setlocale(LC_ALL, $locale . '.utf-8');
setlocale(LC_ALL, $locale . '.UTF-8');
$langPath = $container->get('settings')['i18n']['path']; $langPath = $container->get('settings')['i18n']['path'];
$translator = $container->get('l10n'); $translator = $container->get('l10n');
@ -27,11 +33,19 @@ $middleware->setLocaleCallback(function (string $locale) use ($container) {
$translator->loadDomain('strings', $langPath); $translator->loadDomain('strings', $langPath);
} else { } else {
$lang = substr($locale, 0, 2);
/** @var $translator \Gettext\Translator */ /** @var $translator \Gettext\Translator */
if (file_exists($langPath . '/' . $locale . '/LC_MESSAGES/strings.mo')) { if (file_exists($langPath . '/' . $locale . '/LC_MESSAGES/strings.mo')) {
$translator->loadTranslations(Gettext\Translations::fromMoFile($langPath . '/' . $locale . '/LC_MESSAGES/strings.mo')); $translator->loadTranslations(Gettext\Translations::fromMoFile($langPath . '/' . $locale . '/LC_MESSAGES/strings.mo'));
} elseif (file_exists($langPath . '/' . $locale . '/LC_MESSAGES/strings.po')) { } elseif (file_exists($langPath . '/' . $locale . '/LC_MESSAGES/strings.po')) {
$translator->loadTranslations(Gettext\Translations::fromPoFile($langPath . '/' . $locale . '/LC_MESSAGES/strings.po')); $translator->loadTranslations(Gettext\Translations::fromPoFile($langPath . '/' . $locale . '/LC_MESSAGES/strings.po'));
} elseif (file_exists($langPath . '/' . $lang . '/LC_MESSAGES/strings.mo')) {
// Defaulting to language superset
$translator->loadTranslations(Gettext\Translations::fromMoFile($langPath . '/' . $lang . '/LC_MESSAGES/strings.mo'));
} elseif (file_exists($langPath . '/' . $lang . '/LC_MESSAGES/strings.po')) {
// Defaulting to language superset
$translator->loadTranslations(Gettext\Translations::fromPoFile($langPath . '/' . $lang . '/LC_MESSAGES/strings.po'));
} }
} }
}); });

View file

@ -19,8 +19,8 @@ $settings = [
'displayErrorDetails' => false, // set to false in production 'displayErrorDetails' => false, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header 'addContentLengthHeader' => false, // Allow the web server to send the content-length header
'i18n' => [ 'i18n' => [
'locales' => ['en', 'fr', 'cs'], 'locales' => ['en_US', 'fr_FR', 'cs_CZ'],
'default' => 'en', 'default' => 'en_US',
'path' => __DIR__ . '/lang' 'path' => __DIR__ . '/lang'
], ],
// Escaper settings // Escaper settings

View file

@ -67,7 +67,7 @@
<div class="dropdown-menu" aria-labelledby="languageDropdown"> <div class="dropdown-menu" aria-labelledby="languageDropdown">
<?php foreach($languages as $lang):?> <?php foreach($languages as $lang):?>
<a class="dropdown-item" href="?lang=<?php echo $this->escapeUrl($lang)?>"> <a class="dropdown-item" href="?lang=<?php echo $this->escapeUrl($lang)?>">
<?php echo $this->e(Friendica\Directory\Utils\L10n::langToString($lang)) ?> <?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($lang)) ?>
</a> </a>
<?php endforeach;?> <?php endforeach;?>
</div> </div>

View file

@ -45,7 +45,7 @@ if (!empty($profile['country'])) {
<?php if ($profile['language']):?> <?php if ($profile['language']):?>
<div class="language"> <div class="language">
<i class="fa fa-language" alt="<?php echo $this->__('Language')?>" title="<?php echo $this->__('Language')?>"></i> <i class="fa fa-language" alt="<?php echo $this->__('Language')?>" title="<?php echo $this->__('Language')?>"></i>
<?php echo $this->e(Friendica\Directory\Utils\L10n::langToString($profile['language'])) ?> <?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($profile['language'])) ?>
<a href="/search?field=language&amp;q=<?php echo $this->escapeUrl($profile['language']) ?>"> <a href="/search?field=language&amp;q=<?php echo $this->escapeUrl($profile['language']) ?>">
<i class="fa fa-filter" title="<?php echo $this->__('Filter by language')?>"></i> <i class="fa fa-filter" title="<?php echo $this->__('Filter by language')?>"></i>
</a> </a>

View file

@ -43,7 +43,7 @@ $base_url_display = substr($base_url, strpos($base_url, '/') + 2);
<?php if ($server['language']):?> <?php if ($server['language']):?>
<span class="badge badge-secondary" title="<?php echo $this->__('Default Language')?>"> <span class="badge badge-secondary" title="<?php echo $this->__('Default Language')?>">
<i class="fa fa-language" alt="<?php echo $this->__('Default Language')?>"></i> <i class="fa fa-language" alt="<?php echo $this->__('Default Language')?>"></i>
<?php echo $this->e(Friendica\Directory\Utils\L10n::langToString($server['language'])) ?> <?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($server['language'])) ?>
</span> </span>
<?php endif;?> <?php endif;?>
<span class="badge badge-secondary" title="<?php echo $this->__('Known Users')?>"> <span class="badge badge-secondary" title="<?php echo $this->__('Known Users')?>">

View file

@ -4,7 +4,7 @@
<?php foreach ($languages as $language): ?> <?php foreach ($languages as $language): ?>
<li> <li>
<a href="search?field=language&q=<?php echo $this->escapeUrl($language['language']) ?>"> <a href="search?field=language&q=<?php echo $this->escapeUrl($language['language']) ?>">
<?php echo $this->e(Friendica\Directory\Utils\L10n::langToString($language['language'])) ?> <?php echo $this->e(Friendica\Directory\Utils\L10n::localeToLanguageString($language['language'])) ?>
</a> </a>
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>