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:
parent
012a3f9f8d
commit
e8752c7631
|
@ -10,6 +10,9 @@ use Gettext\Translator;
|
|||
*/
|
||||
class L10n
|
||||
{
|
||||
const DECIMAL = 1;
|
||||
const PERCENT = 2;
|
||||
|
||||
public static $languages = [
|
||||
'af' => 'Afrikaans',
|
||||
'ak' => 'Akan',
|
||||
|
@ -80,6 +83,7 @@ class L10n
|
|||
'ks' => 'کٲشُر',
|
||||
'kw' => 'Kernewek',
|
||||
'ky' => 'Кыргызча',
|
||||
'la' => 'Lingua Latina',
|
||||
'lb' => 'Lëtzebuergesch',
|
||||
'lg' => 'Luganda',
|
||||
'ln' => 'Lingála',
|
||||
|
@ -150,16 +154,62 @@ class L10n
|
|||
'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) {
|
||||
if (strtolower($key) == strtolower(str_replace('-', '_', $lang))) {
|
||||
$found = true;
|
||||
if (strtolower($key) == strtolower($lang)) {
|
||||
$foundLang = $language;
|
||||
}
|
||||
if (strtolower($key) == strtolower(str_replace('-', '_', $locale))) {
|
||||
$foundLocale = true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,12 @@ $middleware = new LocalizationMiddleware(
|
|||
);
|
||||
|
||||
$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'];
|
||||
|
||||
$translator = $container->get('l10n');
|
||||
|
@ -27,11 +33,19 @@ $middleware->setLocaleCallback(function (string $locale) use ($container) {
|
|||
|
||||
$translator->loadDomain('strings', $langPath);
|
||||
} else {
|
||||
$lang = substr($locale, 0, 2);
|
||||
|
||||
/** @var $translator \Gettext\Translator */
|
||||
if (file_exists($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')) {
|
||||
$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'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -19,8 +19,8 @@ $settings = [
|
|||
'displayErrorDetails' => false, // set to false in production
|
||||
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
|
||||
'i18n' => [
|
||||
'locales' => ['en', 'fr', 'cs'],
|
||||
'default' => 'en',
|
||||
'locales' => ['en_US', 'fr_FR', 'cs_CZ'],
|
||||
'default' => 'en_US',
|
||||
'path' => __DIR__ . '/lang'
|
||||
],
|
||||
// Escaper settings
|
||||
|
|
|
@ -67,7 +67,7 @@
|
|||
<div class="dropdown-menu" aria-labelledby="languageDropdown">
|
||||
<?php foreach($languages as $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>
|
||||
<?php endforeach;?>
|
||||
</div>
|
||||
|
|
|
@ -45,7 +45,7 @@ if (!empty($profile['country'])) {
|
|||
<?php if ($profile['language']):?>
|
||||
<div class="language">
|
||||
<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&q=<?php echo $this->escapeUrl($profile['language']) ?>">
|
||||
<i class="fa fa-filter" title="<?php echo $this->__('Filter by language')?>"></i>
|
||||
</a>
|
||||
|
|
|
@ -43,7 +43,7 @@ $base_url_display = substr($base_url, strpos($base_url, '/') + 2);
|
|||
<?php if ($server['language']):?>
|
||||
<span class="badge badge-secondary" title="<?php echo $this->__('Default Language')?>">
|
||||
<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>
|
||||
<?php endif;?>
|
||||
<span class="badge badge-secondary" title="<?php echo $this->__('Known Users')?>">
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<?php foreach ($languages as $language): ?>
|
||||
<li>
|
||||
<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>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
|
|
Loading…
Reference in a new issue