summaryrefslogtreecommitdiff
path: root/src/Helpers/TranslationServiceProvider.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-08 12:48:08 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-09 12:11:12 +0200
commit222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (patch)
tree62b1bfa9233000985331b9dea703d51dc24515a2 /src/Helpers/TranslationServiceProvider.php
parent2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff)
parent8257864829ffdfb410f05e0dd0a9c781f48b741a (diff)
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'src/Helpers/TranslationServiceProvider.php')
-rw-r--r--src/Helpers/TranslationServiceProvider.php58
1 files changed, 58 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);
+ }
+}