diff --git a/Plugin.php b/Plugin.php index 17dba879..ca55783a 100644 --- a/Plugin.php +++ b/Plugin.php @@ -238,6 +238,8 @@ public function registerMarkupTags() 'filters' => [ '_' => [$this, 'translateString'], '__' => [$this, 'translatePlural'], + 'transRaw' => [$this, 'translateRawString'], + 'transRawPlural' => [$this, 'translateRawPlural'], 'localeUrl' => [$this, 'localeUrl'], ] ]; @@ -282,4 +284,14 @@ public function translatePlural($string, $count = 0, $params = [], $locale = nul { return Lang::choice(Message::trans($string, $params, $locale), $count, $params); } + + public function translateRawString($string, $params = [], $locale = null) + { + return Message::transRaw($string, $params, $locale); + } + + public function translateRawPlural($string, $count = 0, $params = [], $locale = null) + { + return Lang::choice(Message::transRaw($string, $params, $locale), $count, $params); + } } diff --git a/models/Message.php b/models/Message.php index 83033ee9..c9f71049 100644 --- a/models/Message.php +++ b/models/Message.php @@ -204,6 +204,26 @@ public static function trans($messageId, $params = [], $locale = null) { $msg = static::get($messageId, $locale); + $params = array_build($params, function($key, $value){ + return [':'.$key, e($value)]; + }); + + $msg = strtr($msg, $params); + + return $msg; + } + + /** + * Looks up and translates a message by its string WITHOUT escaping params. + * @param string $messageId + * @param array $params + * @param string $locale + * @return string + */ + public static function transRaw($messageId, $params = [], $locale = null) + { + $msg = static::get($messageId, $locale); + $params = array_build($params, function($key, $value){ return [':'.$key, $value]; });