diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-10-18 23:34:18 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2018-10-30 22:50:22 +0100 |
commit | b443b53919f50bd0176e7b67dfd1efc28276a770 (patch) | |
tree | df4d089d9d431f5223a71e6b40f9d87361705929 /src | |
parent | 90e1a949623ead173c0952f802d7b5c5487251b1 (diff) |
Translation: added pluralization support
Diffstat (limited to 'src')
-rw-r--r-- | src/Helpers/Translator.php | 50 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Translation.php | 1 | ||||
-rw-r--r-- | src/helpers.php | 17 |
3 files changed, 64 insertions, 4 deletions
diff --git a/src/Helpers/Translator.php b/src/Helpers/Translator.php index 1e953a21..94fbd795 100644 --- a/src/Helpers/Translator.php +++ b/src/Helpers/Translator.php @@ -39,11 +39,39 @@ class Translator { $translated = $this->translateGettext($key); - if (!empty($replace)) { - $translated = call_user_func_array('sprintf', array_merge([$translated], $replace)); + 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 $translated; + return call_user_func_array('sprintf', array_merge([$key], $replace)); } /** @@ -55,7 +83,21 @@ class Translator */ protected function translateGettext(string $key): string { - return _($key); + 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); } /** diff --git a/src/Renderer/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php index 63f9800e..41619c19 100644 --- a/src/Renderer/Twig/Extensions/Translation.php +++ b/src/Renderer/Twig/Extensions/Translation.php @@ -44,6 +44,7 @@ class Translation extends TwigExtension { return [ new TwigFunction('__', [$this->translator, 'translate']), + new TwigFunction('_e', [$this->translator, 'translatePlural']), ]; } diff --git a/src/helpers.php b/src/helpers.php index 84f26dfa..64ca9ec8 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -154,6 +154,23 @@ function __($key, $replace = []) } /** + * Translate the given message + * + * @param string $key + * @param string $keyPlural + * @param int $number + * @param array $replace + * @return string + */ +function _e($key, $keyPlural, $number, $replace = []) +{ + /** @var Translator $translator */ + $translator = app('translator'); + + return $translator->translatePlural($key, $keyPlural, $number, $replace); +} + +/** * @param string $path * @param array $parameters * @return UrlGeneratorInterface|string |