Skip to content

Commit

Permalink
Message translation with locale argument (#406)
Browse files Browse the repository at this point in the history
extend message translation to use locale argument
  • Loading branch information
munxar authored and mjauvin committed Dec 11, 2019
1 parent a56ac90 commit 3ba3a85
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 11 additions & 8 deletions models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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];
}

/*
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 3ba3a85

Please sign in to comment.