From 3ba3a85ba896ce950324e0f4302b698ba9327640 Mon Sep 17 00:00:00 2001 From: munxar Date: Wed, 11 Dec 2019 02:55:59 +0100 Subject: [PATCH] Message translation with locale argument (#406) extend message translation to use locale argument --- Plugin.php | 8 ++++---- README.md | 4 ++++ models/Message.php | 19 +++++++++++-------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Plugin.php b/Plugin.php index 27e0ff6c..7e625f94 100644 --- a/Plugin.php +++ b/Plugin.php @@ -201,13 +201,13 @@ public function localeUrl($url, $locale) ]); } - public function translateString($string, $params = []) + public function translateString($string, $params = [], $locale = null) { - return Message::trans($string, $params); + return Message::trans($string, $params, $locale); } - public function translatePlural($string, $count = 0, $params = []) + public function translatePlural($string, $count = 0, $params = [], $locale = null) { - return Lang::choice(Message::trans($string, $params), $count, $params); + return Lang::choice(Message::trans($string, $params, $locale), $count, $params); } } diff --git a/README.md b/README.md index 8369dd4d..e1b8e42b 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ A message can also be translated for a choice usage. {{ 'There are no apples|There are :number applies!'|__(2, { number: 'two' }) }} +Or you set a locale manually by passing a second argument. + + {{ 'this is always english'|_({}, 'en') }} + Themes can provide default values for these messages by defining a `translate` key in the `theme.yaml` file, located in the theme directory. name: My Theme diff --git a/models/Message.php b/models/Message.php index 74503adf..83033ee9 100644 --- a/models/Message.php +++ b/models/Message.php @@ -94,11 +94,13 @@ public function toLocale($locale = null, $message) /** * Creates or finds an untranslated message string. * @param string $messageId + * @param string $locale * @return string */ - public static function get($messageId) + public static function get($messageId, $locale = null) { - if (!self::$locale) { + $locale = $locale ?: self::$locale; + if (!$locale) { return $messageId; } @@ -107,8 +109,8 @@ public static function get($messageId) /* * Found in cache */ - if (array_key_exists(self::$locale . $messageCode, self::$cache)) { - return self::$cache[self::$locale . $messageCode]; + if (array_key_exists($locale . $messageCode, self::$cache)) { + return self::$cache[$locale . $messageCode]; } /* @@ -130,8 +132,8 @@ public static function get($messageId) /* * Schedule new cache and go */ - $msg = $item->forLocale(self::$locale, $messageId); - self::$cache[self::$locale . $messageCode] = $msg; + $msg = $item->forLocale($locale, $messageId); + self::$cache[$locale . $messageCode] = $msg; self::$hasNew = true; return $msg; @@ -195,11 +197,12 @@ public static function importMessageCodes($messages, $locale = null) * Looks up and translates a message by its string. * @param string $messageId * @param array $params + * @param string $locale * @return string */ - public static function trans($messageId, $params = []) + public static function trans($messageId, $params = [], $locale = null) { - $msg = static::get($messageId); + $msg = static::get($messageId, $locale); $params = array_build($params, function($key, $value){ return [':'.$key, $value];