From e8752c76318163273f81f8dbf3cf6f55ea8c172d Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 18 Nov 2018 10:38:17 -0500 Subject: [PATCH] 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 --- src/classes/Utils/L10n.php | 60 +++++++++++++++++++-- src/middleware.php | 14 +++++ src/settings.php | 4 +- src/templates/layout.phtml | 2 +- src/templates/sub/profile.phtml | 2 +- src/templates/sub/server.phtml | 2 +- src/templates/widget/popularlanguages.phtml | 2 +- 7 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/classes/Utils/L10n.php b/src/classes/Utils/L10n.php index 5439d18..96de65b 100644 --- a/src/classes/Utils/L10n.php +++ b/src/classes/Utils/L10n.php @@ -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; } } diff --git a/src/middleware.php b/src/middleware.php index dc000a9..ce41504 100644 --- a/src/middleware.php +++ b/src/middleware.php @@ -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')); } } }); diff --git a/src/settings.php b/src/settings.php index c5e28fa..3f47a25 100644 --- a/src/settings.php +++ b/src/settings.php @@ -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 diff --git a/src/templates/layout.phtml b/src/templates/layout.phtml index 8b9108a..54a08d9 100644 --- a/src/templates/layout.phtml +++ b/src/templates/layout.phtml @@ -67,7 +67,7 @@ diff --git a/src/templates/sub/profile.phtml b/src/templates/sub/profile.phtml index 032b761..2dcbc1e 100644 --- a/src/templates/sub/profile.phtml +++ b/src/templates/sub/profile.phtml @@ -45,7 +45,7 @@ if (!empty($profile['country'])) {
- e(Friendica\Directory\Utils\L10n::langToString($profile['language'])) ?> + e(Friendica\Directory\Utils\L10n::localeToLanguageString($profile['language'])) ?> diff --git a/src/templates/sub/server.phtml b/src/templates/sub/server.phtml index 766a0e7..f8adf0c 100644 --- a/src/templates/sub/server.phtml +++ b/src/templates/sub/server.phtml @@ -43,7 +43,7 @@ $base_url_display = substr($base_url, strpos($base_url, '/') + 2); - e(Friendica\Directory\Utils\L10n::langToString($server['language'])) ?> + e(Friendica\Directory\Utils\L10n::localeToLanguageString($server['language'])) ?> diff --git a/src/templates/widget/popularlanguages.phtml b/src/templates/widget/popularlanguages.phtml index 5c06216..15fa128 100644 --- a/src/templates/widget/popularlanguages.phtml +++ b/src/templates/widget/popularlanguages.phtml @@ -4,7 +4,7 @@
  • - e(Friendica\Directory\Utils\L10n::langToString($language['language'])) ?> + e(Friendica\Directory\Utils\L10n::localeToLanguageString($language['language'])) ?>