summaryrefslogtreecommitdiff
path: root/src/Helpers
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
parent2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff)
parent8257864829ffdfb410f05e0dd0a9c781f48b741a (diff)
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'src/Helpers')
-rw-r--r--src/Helpers/TranslationServiceProvider.php58
-rw-r--r--src/Helpers/Translator.php105
2 files changed, 163 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;
+ }
+}