1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Translator; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Represents message category. |
11
|
|
|
*/ |
12
|
|
|
final class CategorySource |
13
|
|
|
{ |
14
|
|
|
private string $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $name Category name. |
18
|
|
|
* @param MessageReaderInterface $reader Message reader to get messages from for this category. |
19
|
|
|
* @param MessageFormatterInterface|null $formatter Message formatter to format messages with for this category. |
20
|
|
|
*/ |
21
|
59 |
|
public function __construct( |
22
|
|
|
string $name, |
23
|
|
|
private MessageReaderInterface $reader, |
24
|
|
|
private ?MessageFormatterInterface $formatter = null |
25
|
|
|
) { |
26
|
59 |
|
if (!preg_match('/^[a-z0-9_-]+$/i', $name)) { |
27
|
1 |
|
throw new RuntimeException('Category name is invalid. Only letters and numbers are allowed.'); |
28
|
|
|
} |
29
|
58 |
|
$this->name = $name; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string Category name. |
34
|
|
|
*/ |
35
|
56 |
|
public function getName(): string |
36
|
|
|
{ |
37
|
56 |
|
return $this->name; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a message with ID, locale and parameters specified. |
42
|
|
|
* |
43
|
|
|
* @param string $id Message ID. |
44
|
|
|
* @param string $locale Locale to get message for. |
45
|
|
|
* @param array $parameters Message parameters. |
46
|
|
|
* |
47
|
|
|
* @return string|null Message string or null if message was not found. |
48
|
|
|
*/ |
49
|
53 |
|
public function getMessage(string $id, string $locale, array $parameters = []): ?string |
50
|
|
|
{ |
51
|
53 |
|
return $this->reader->getMessage($id, $this->name, $locale, $parameters); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $locale Locale of messages to get. |
56
|
|
|
* |
57
|
|
|
* @psalm-return array<string, array<string, string>> |
58
|
|
|
* |
59
|
|
|
* @return array All messages from category. The format is the following: |
60
|
|
|
* |
61
|
|
|
* ```php |
62
|
|
|
* [ |
63
|
|
|
* 'key1' => [ |
64
|
|
|
* 'message' => 'translation1', |
65
|
|
|
* // Extra metadata that writer may use: |
66
|
|
|
* 'comment' => 'Translate carefully!', |
67
|
|
|
* ], |
68
|
|
|
* 'key2' => [ |
69
|
|
|
* 'message' => 'translation2', |
70
|
|
|
* // Extra metadata that writer may use: |
71
|
|
|
* 'comment' => 'Translate carefully!', |
72
|
|
|
* ], |
73
|
|
|
* ] |
74
|
|
|
* ``` |
75
|
|
|
* |
76
|
|
|
* @see MessageReaderInterface::getMessages() |
77
|
|
|
*/ |
78
|
1 |
|
public function getMessages(string $locale): array |
79
|
|
|
{ |
80
|
1 |
|
return $this->reader->getMessages($this->name, $locale); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Format the message given parameters and locale. |
85
|
|
|
* |
86
|
|
|
* @param string $message Message to be formatted. |
87
|
|
|
* @param array $parameters Parameters to use. |
88
|
|
|
* @param string $locale Locale to use. Usually affects formatting numbers, dates etc. |
89
|
|
|
* @param MessageFormatterInterface $defaultFormatter Message formatter that will be used if formatter not specified |
90
|
|
|
* in message category. |
91
|
|
|
* |
92
|
|
|
* @return string Formatted message. |
93
|
|
|
*/ |
94
|
55 |
|
public function format( |
95
|
|
|
string $message, |
96
|
|
|
array $parameters, |
97
|
|
|
string $locale, |
98
|
|
|
MessageFormatterInterface $defaultFormatter |
99
|
|
|
): string { |
100
|
55 |
|
return ($this->formatter ?? $defaultFormatter)->format($message, $parameters, $locale); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|