diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Helpers/TranslationServiceProvider.php | 58 | ||||
-rw-r--r-- | src/Helpers/Translator.php | 105 | ||||
-rw-r--r-- | src/Middleware/SetLocale.php | 49 | ||||
-rw-r--r-- | src/helpers.php | 35 |
4 files changed, 247 insertions, 0 deletions
diff --git a/src/Helpers/TranslationServiceProvider.php b/src/Helpers/TranslationServiceProvider.php new file mode 100644 index 00000000..9d86df7d --- /dev/null +++ b/src/Helpers/TranslationServiceProvider.php @@ -0,0 +1,58 @@ +<?php + +namespace Engelsystem\Helpers; + +use Engelsystem\Config\Config; +use Engelsystem\Container\ServiceProvider; +use Symfony\Component\HttpFoundation\Session\Session; + +class TranslationServiceProvider extends ServiceProvider +{ + public function register() + { + /** @var Config $config */ + $config = $this->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) + { + putenv('LC_ALL=' . $locale); + setlocale(LC_ALL, $locale); + } +} diff --git a/src/Helpers/Translator.php b/src/Helpers/Translator.php new file mode 100644 index 00000000..1e953a21 --- /dev/null +++ b/src/Helpers/Translator.php @@ -0,0 +1,105 @@ +<?php + +namespace Engelsystem\Helpers; + +class Translator +{ + /** @var string[] */ + protected $locales; + + /** @var string */ + protected $locale; + + /** @var callable */ + protected $localeChangeCallback; + + /** + * Translator constructor. + * + * @param string $locale + * @param string[] $locales + * @param callable $localeChangeCallback + */ + public function __construct(string $locale, array $locales = [], callable $localeChangeCallback = null) + { + $this->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); + + if (!empty($replace)) { + $translated = call_user_func_array('sprintf', array_merge([$translated], $replace)); + } + + return $translated; + } + + /** + * Translate the key via gettext + * + * @param string $key + * @return string + * @codeCoverageIgnore + */ + protected function translateGettext(string $key): string + { + return _($key); + } + + /** + * @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; + } +} diff --git a/src/Middleware/SetLocale.php b/src/Middleware/SetLocale.php new file mode 100644 index 00000000..86fa0b7f --- /dev/null +++ b/src/Middleware/SetLocale.php @@ -0,0 +1,49 @@ +<?php + +namespace Engelsystem\Middleware; + +use Engelsystem\Helpers\Translator; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpFoundation\Session\Session; + +class SetLocale implements MiddlewareInterface +{ + /** @var Translator */ + protected $translator; + + /** @var Session */ + protected $session; + + /** + * @param Translator $translator + * @param Session $session + */ + public function __construct(Translator $translator, Session $session) + { + $this->translator = $translator; + $this->session = $session; + } + + /** + * Process an incoming server request and setting the locale if required + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + $query = $request->getQueryParams(); + if (isset($query['set-locale']) && $this->translator->hasLocale($query['set-locale'])) { + $locale = $query['set-locale']; + + $this->translator->setLocale($locale); + $this->session->set('locale', $locale); + } + + return $handler->handle($request); + } +} diff --git a/src/helpers.php b/src/helpers.php index 336f81fe..3a182bf7 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -3,6 +3,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; +use Engelsystem\Helpers\Translator; use Engelsystem\Http\Request; use Engelsystem\Http\Response; use Engelsystem\Http\UrlGenerator; @@ -119,6 +120,40 @@ function session($key = null, $default = null) } /** + * Translate the given message + * + * @param string $key + * @param array $replace + * @return string|Translator + */ +function trans($key = null, $replace = []) +{ + /** @var Translator $translator */ + $translator = app('translator'); + + if (is_null($key)) { + return $translator; + } + + return $translator->translate($key, $replace); +} + +/** + * Translate the given message + * + * @param string $key + * @param array $replace + * @return string + */ +function __($key, $replace = []) +{ + /** @var Translator $translator */ + $translator = app('translator'); + + return $translator->translate($key, $replace); +} + +/** * @param string $path * @param array $parameters * @return UrlGenerator|string |