summaryrefslogtreecommitdiff
path: root/src/Helpers
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-10-18 23:34:18 +0200
committermsquare <msquare@notrademark.de>2018-10-30 22:50:22 +0100
commitb443b53919f50bd0176e7b67dfd1efc28276a770 (patch)
treedf4d089d9d431f5223a71e6b40f9d87361705929 /src/Helpers
parent90e1a949623ead173c0952f802d7b5c5487251b1 (diff)
Translation: added pluralization support
Diffstat (limited to 'src/Helpers')
-rw-r--r--src/Helpers/Translator.php50
1 files changed, 46 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);
}
/**