From f90ab26feedb61615bde2f94bbf5acc7e4f28342 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 8 Jul 2019 01:31:59 +0200 Subject: Moved translation helpers to sub namespace --- .../Translation/TranslationServiceProvider.php | 63 +++++++++ src/Helpers/Translation/Translator.php | 147 +++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 src/Helpers/Translation/TranslationServiceProvider.php create mode 100644 src/Helpers/Translation/Translator.php (limited to 'src/Helpers/Translation') diff --git a/src/Helpers/Translation/TranslationServiceProvider.php b/src/Helpers/Translation/TranslationServiceProvider.php new file mode 100644 index 00000000..d0cda6a8 --- /dev/null +++ b/src/Helpers/Translation/TranslationServiceProvider.php @@ -0,0 +1,63 @@ +app->get('config'); + /** @var Session $session */ + $session = $this->app->get('session'); + + $locales = $config->get('locales'); + $locale = $config->get('default_locale'); + + $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']] + ); + $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) + { + // Set the users locale + putenv('LC_ALL=' . $locale); + setlocale(LC_ALL, $locale); + + // Reset numeric formatting to allow output of floats + putenv('LC_NUMERIC=C'); + setlocale(LC_NUMERIC, 'C'); + } +} diff --git a/src/Helpers/Translation/Translator.php b/src/Helpers/Translation/Translator.php new file mode 100644 index 00000000..545963eb --- /dev/null +++ b/src/Helpers/Translation/Translator.php @@ -0,0 +1,147 @@ +localeChangeCallback = $localeChangeCallback; + + $this->setLocale($locale); + $this->setLocales($locales); + } + + /** + * Get the translation for a given key + * + * @param string $key + * @param array $replace + * @return string + */ + public function translate(string $key, array $replace = []): string + { + $translated = $this->translateGettext($key); + + return $this->replaceText($translated, $replace); + } + + /** + * Get the translation for a given key + * + * @param string $key + * @param string $pluralKey + * @param int $number + * @param array $replace + * @return string + */ + public function translatePlural(string $key, string $pluralKey, int $number, array $replace = []): string + { + $translated = $this->translateGettextPlural($key, $pluralKey, $number); + + return $this->replaceText($translated, $replace); + } + + /** + * Replace placeholders + * + * @param string $key + * @param array $replace + * @return mixed|string + */ + protected function replaceText(string $key, array $replace = []) + { + if (empty($replace)) { + return $key; + } + + return call_user_func_array('sprintf', array_merge([$key], $replace)); + } + + /** + * 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 + { + return $this->locale; + } + + /** + * @param string $locale + */ + public function setLocale(string $locale) + { + $this->locale = $locale; + + if (is_callable($this->localeChangeCallback)) { + call_user_func_array($this->localeChangeCallback, [$locale]); + } + } + + /** + * @return string[] + */ + public function getLocales(): array + { + return $this->locales; + } + + /** + * @param string $locale + * @return bool + */ + public function hasLocale(string $locale): bool + { + return isset($this->locales[$locale]); + } + + /** + * @param string[] $locales + */ + public function setLocales(array $locales) + { + $this->locales = $locales; + } +} -- cgit v1.2.3-54-g00ecf