From b443b53919f50bd0176e7b67dfd1efc28276a770 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Thu, 18 Oct 2018 23:34:18 +0200 Subject: Translation: added pluralization support --- src/Helpers/Translator.php | 50 +++++++++++++++++++++++++--- src/Renderer/Twig/Extensions/Translation.php | 1 + src/helpers.php | 17 ++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) (limited to 'src') 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 @@ -153,6 +153,23 @@ function __($key, $replace = []) return $translator->translate($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 -- cgit v1.2.3-70-g09d2