Skip to content

Commit dd96cd1

Browse files
xepozzarogachevvjik
authored
Add Stringable type support for $id argument in Translator::translate() (#70)
* Add Stringable type to message * Remove php 7.4 support * Type cast was in a wrong place * Add changelog entry * PHPDoc - fix max line length, delete extra whitespace * Fix CHANGELOG according to BC break Co-authored-by: Alexey Rogachev <arogachev90@gmail.com> Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
1 parent e1b78ce commit dd96cd1

3 files changed

Lines changed: 12 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Yii Message Translator Change Log
22

3-
## 1.1.2 under development
3+
## 2.0.0 under development
44

5+
- Enh #70: Add `Stringable` type support for `$id` argument in `Translator::translate()` (@xepozz)
56
- Enh #74: Dispatch `MissingTranslationCategoryEvent` once per category (@vjik)
67
- Enh #73: Set `en_US` as default locale for translator (@vjik)
78
- Enh #72, #75: Format messages when missing translation category (@vjik)

src/Translator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psr\EventDispatcher\EventDispatcherInterface;
88
use RuntimeException;
9+
use Stringable;
910
use Yiisoft\I18n\Locale;
1011
use Yiisoft\Translator\Event\MissingTranslationCategoryEvent;
1112
use Yiisoft\Translator\Event\MissingTranslationEvent;
@@ -71,7 +72,7 @@ public function getLocale(): string
7172
}
7273

7374
public function translate(
74-
string $id,
75+
string|Stringable $id,
7576
array $parameters = [],
7677
string $category = null,
7778
string $locale = null
@@ -82,10 +83,10 @@ public function translate(
8283

8384
if (empty($this->categorySources[$category])) {
8485
$this->dispatchMissingTranslationCategoryEvent($category);
85-
return $this->defaultMessageFormatter->format($id, $parameters, $locale);
86+
return $this->defaultMessageFormatter->format((string) $id, $parameters, $locale);
8687
}
8788

88-
return $this->translateUsingCategorySources($id, $parameters, $category, $locale);
89+
return $this->translateUsingCategorySources((string) $id, $parameters, $category, $locale);
8990
}
9091

9192
/**

src/TranslatorInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Yiisoft\Translator;
66

7+
use Stringable;
8+
79
/**
810
* Translator translates a message into the specified language.
911
*/
@@ -34,8 +36,9 @@ public function getLocale(): string;
3436
/**
3537
* Translates a message into the specified language.
3638
*
37-
* @param string $id The ID of the message to be translated. It can be either artificial ID or the source message.
38-
* @param array $parameters An array of parameters for the message.
39+
* @param string|Stringable $id The ID of the message to be translated. It can be either artificial ID or the source
40+
* message.
41+
* @param array $parameters An array of parameters for the message.
3942
* @psalm-param array<array-key, mixed> $parameters
4043
*
4144
* @param string|null $category The message category. Null means default category.
@@ -44,7 +47,7 @@ public function getLocale(): string;
4447
* @return string The translated message or source string ID if translation was not found or is not required.
4548
*/
4649
public function translate(
47-
string $id,
50+
string|Stringable $id,
4851
array $parameters = [],
4952
string $category = null,
5053
string $locale = null

0 commit comments

Comments
 (0)