diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-08 01:31:59 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2019-07-08 01:58:06 +0200 |
commit | f90ab26feedb61615bde2f94bbf5acc7e4f28342 (patch) | |
tree | bef3b8c72e27089dbef317512c4ab2d11ab34405 /src/Helpers/Translator.php | |
parent | e9f157ec5ccdfae73b4c9e82c9ae7c37bcfa1513 (diff) |
Moved translation helpers to sub namespace
Diffstat (limited to 'src/Helpers/Translator.php')
-rw-r--r-- | src/Helpers/Translator.php | 147 |
1 files changed, 0 insertions, 147 deletions
diff --git a/src/Helpers/Translator.php b/src/Helpers/Translator.php deleted file mode 100644 index 94fbd795..00000000 --- a/src/Helpers/Translator.php +++ /dev/null @@ -1,147 +0,0 @@ -<?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); - - 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; - } -} |