diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Helpers/Translation/GettextTranslator.php | 53 | ||||
-rw-r--r-- | src/Helpers/Translation/TranslationNotFound.php | 9 | ||||
-rw-r--r-- | src/Helpers/Translation/TranslationServiceProvider.php | 55 | ||||
-rw-r--r-- | src/Helpers/Translation/Translator.php | 75 |
4 files changed, 143 insertions, 49 deletions
diff --git a/src/Helpers/Translation/GettextTranslator.php b/src/Helpers/Translation/GettextTranslator.php new file mode 100644 index 00000000..7f2299e2 --- /dev/null +++ b/src/Helpers/Translation/GettextTranslator.php @@ -0,0 +1,53 @@ +<?php + +namespace Engelsystem\Helpers\Translation; + +use Gettext\Translator; + +class GettextTranslator extends Translator +{ + /** + * @param string $domain + * @param string $context + * @param string $original + * @return string + * @throws TranslationNotFound + */ + public function dpgettext($domain, $context, $original) + { + $this->assertHasTranslation($domain, $context, $original); + + return parent::dpgettext($domain, $context, $original); + } + + /** + * @param string $domain + * @param string $context + * @param string $original + * @param string $plural + * @param string $value + * @return string + * @throws TranslationNotFound + */ + public function dnpgettext($domain, $context, $original, $plural, $value) + { + $this->assertHasTranslation($domain, $context, $original); + + return parent::dnpgettext($domain, $context, $original, $plural, $value); + } + + /** + * @param string $domain + * @param string $context + * @param string $original + * @throws TranslationNotFound + */ + protected function assertHasTranslation($domain, $context, $original) + { + if ($this->getTranslation($domain, $context, $original)) { + return; + } + + throw new TranslationNotFound(implode('/', [$domain, $context, $original])); + } +} diff --git a/src/Helpers/Translation/TranslationNotFound.php b/src/Helpers/Translation/TranslationNotFound.php new file mode 100644 index 00000000..1552838b --- /dev/null +++ b/src/Helpers/Translation/TranslationNotFound.php @@ -0,0 +1,9 @@ +<?php + +namespace Engelsystem\Helpers\Translation; + +use Exception; + +class TranslationNotFound extends Exception +{ +} diff --git a/src/Helpers/Translation/TranslationServiceProvider.php b/src/Helpers/Translation/TranslationServiceProvider.php index d0cda6a8..09337dad 100644 --- a/src/Helpers/Translation/TranslationServiceProvider.php +++ b/src/Helpers/Translation/TranslationServiceProvider.php @@ -4,11 +4,15 @@ namespace Engelsystem\Helpers\Translation; use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; +use Gettext\Translations; use Symfony\Component\HttpFoundation\Session\Session; class TranslationServiceProvider extends ServiceProvider { - public function register() + /** @var GettextTranslator */ + protected $translators = []; + + public function register(): void { /** @var Config $config */ $config = $this->app->get('config'); @@ -17,41 +21,36 @@ class TranslationServiceProvider extends ServiceProvider $locales = $config->get('locales'); $locale = $config->get('default_locale'); + $fallbackLocale = $config->get('fallback_locale', 'en_US'); $sessionLocale = $session->get('locale', $locale); if (isset($locales[$sessionLocale])) { $locale = $sessionLocale; } - $this->initGettext(); $session->set('locale', $locale); $translator = $this->app->make( Translator::class, - ['locale' => $locale, 'locales' => $locales, 'localeChangeCallback' => [$this, 'setLocale']] + [ + 'locale' => $locale, + 'locales' => $locales, + 'fallbackLocale' => $fallbackLocale, + 'getTranslatorCallback' => [$this, 'getTranslator'], + 'localeChangeCallback' => [$this, 'setLocale'], + ] ); $this->app->instance(Translator::class, $translator); $this->app->instance('translator', $translator); } /** - * @param string $textDomain - * @param string $encoding - * @codeCoverageIgnore - */ - protected function initGettext($textDomain = 'default', $encoding = 'UTF-8') - { - bindtextdomain($textDomain, $this->app->get('path.lang')); - bind_textdomain_codeset($textDomain, $encoding); - textdomain($textDomain); - } - - /** * @param string $locale * @codeCoverageIgnore */ - public function setLocale($locale) + public function setLocale(string $locale): void { + $locale .= '.UTF-8'; // Set the users locale putenv('LC_ALL=' . $locale); setlocale(LC_ALL, $locale); @@ -60,4 +59,28 @@ class TranslationServiceProvider extends ServiceProvider putenv('LC_NUMERIC=C'); setlocale(LC_NUMERIC, 'C'); } + + /** + * @param string $locale + * @return GettextTranslator + */ + public function getTranslator(string $locale): GettextTranslator + { + if (!isset($this->translators[$locale])) { + $file = $this->app->get('path.lang') . '/' . $locale . '/default.mo'; + + /** @var GettextTranslator $translator */ + $translator = $this->app->make(GettextTranslator::class); + + /** @var Translations $translations */ + $translations = $this->app->make(Translations::class); + $translations->addFromMoFile($file); + + $translator->loadTranslations($translations); + + $this->translators[$locale] = $translator; + } + + return $this->translators[$locale]; + } } diff --git a/src/Helpers/Translation/Translator.php b/src/Helpers/Translation/Translator.php index 545963eb..8b11ecb4 100644 --- a/src/Helpers/Translation/Translator.php +++ b/src/Helpers/Translation/Translator.php @@ -10,6 +10,12 @@ class Translator /** @var string */ protected $locale; + /** @var string */ + protected $fallbackLocale; + + /** @var callable */ + protected $getTranslatorCallback; + /** @var callable */ protected $localeChangeCallback; @@ -17,15 +23,24 @@ class Translator * Translator constructor. * * @param string $locale + * @param string $fallbackLocale + * @param callable $getTranslatorCallback * @param string[] $locales * @param callable $localeChangeCallback */ - public function __construct(string $locale, array $locales = [], callable $localeChangeCallback = null) - { + public function __construct( + string $locale, + string $fallbackLocale, + callable $getTranslatorCallback, + array $locales = [], + callable $localeChangeCallback = null + ) { $this->localeChangeCallback = $localeChangeCallback; + $this->getTranslatorCallback = $getTranslatorCallback; $this->setLocale($locale); - $this->setLocales($locales); + $this->fallbackLocale = $fallbackLocale; + $this->locales = $locales; } /** @@ -37,9 +52,7 @@ class Translator */ public function translate(string $key, array $replace = []): string { - $translated = $this->translateGettext($key); - - return $this->replaceText($translated, $replace); + return $this->translateText('gettext', [$key], $replace); } /** @@ -53,7 +66,29 @@ class Translator */ public function translatePlural(string $key, string $pluralKey, int $number, array $replace = []): string { - $translated = $this->translateGettextPlural($key, $pluralKey, $number); + return $this->translateText('ngettext', [$key, $pluralKey, $number], $replace); + } + + /** + * @param string $type + * @param array $parameters + * @param array $replace + * @return mixed|string + */ + protected function translateText(string $type, array $parameters, array $replace = []) + { + $translated = $parameters[0]; + + foreach ([$this->locale, $this->fallbackLocale] as $lang) { + /** @var GettextTranslator $translator */ + $translator = call_user_func($this->getTranslatorCallback, $lang); + + try { + $translated = call_user_func_array([$translator, $type], $parameters); + break; + } catch (TranslationNotFound $e) { + } + } return $this->replaceText($translated, $replace); } @@ -75,32 +110,6 @@ class Translator } /** - * Translate the key via gettext - * - * @param string $key - * @return string - * @codeCoverageIgnore - */ - protected function translateGettext(string $key): string - { - return gettext($key); - } - - /** - * Translate the key via gettext - * - * @param string $key - * @param string $keyPlural - * @param int $number - * @return string - * @codeCoverageIgnore - */ - protected function translateGettextPlural(string $key, string $keyPlural, int $number): string - { - return ngettext($key, $keyPlural, $number); - } - - /** * @return string */ public function getLocale(): string |