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
|
|
|
* @param MessageWriterInterface|null $writer Message writer to write messages for this category. |
21
|
|
|
*/ |
22
|
63 |
|
public function __construct( |
23
|
|
|
string $name, |
24
|
|
|
private MessageReaderInterface $reader, |
25
|
|
|
private ?MessageFormatterInterface $formatter = null, |
26
|
|
|
private ?MessageWriterInterface $writer = null, |
27
|
|
|
) { |
28
|
63 |
|
if (!preg_match('/^[a-z0-9_-]+$/i', $name)) { |
29
|
1 |
|
throw new RuntimeException('Category name is invalid. Only letters and numbers are allowed.'); |
30
|
|
|
} |
31
|
|
|
|
32
|
62 |
|
$this->name = $name; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string Category name. |
37
|
|
|
*/ |
38
|
58 |
|
public function getName(): string |
39
|
|
|
{ |
40
|
58 |
|
return $this->name; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get a message with ID, locale and parameters specified. |
45
|
|
|
* |
46
|
|
|
* @param string $id Message ID. |
47
|
|
|
* @param string $locale Locale to get message for. |
48
|
|
|
* @param array $parameters Message parameters. |
49
|
|
|
* |
50
|
|
|
* @return string|null Message string or null if message was not found. |
51
|
|
|
*/ |
52
|
54 |
|
public function getMessage(string $id, string $locale, array $parameters = []): ?string |
53
|
|
|
{ |
54
|
54 |
|
return $this->reader->getMessage($id, $this->name, $locale, $parameters); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $locale Locale of messages to get. |
59
|
|
|
* |
60
|
|
|
* @psalm-return array<string, array<string, string>> |
61
|
|
|
* |
62
|
|
|
* @return array All messages from category. The format is the following: |
63
|
|
|
* |
64
|
|
|
* ```php |
65
|
|
|
* [ |
66
|
|
|
* 'key1' => [ |
67
|
|
|
* 'message' => 'translation1', |
68
|
|
|
* // Extra metadata that writer may use: |
69
|
|
|
* 'comment' => 'Translate carefully!', |
70
|
|
|
* ], |
71
|
|
|
* 'key2' => [ |
72
|
|
|
* 'message' => 'translation2', |
73
|
|
|
* // Extra metadata that writer may use: |
74
|
|
|
* 'comment' => 'Translate carefully!', |
75
|
|
|
* ], |
76
|
|
|
* ] |
77
|
|
|
* ``` |
78
|
|
|
* |
79
|
|
|
* @see MessageReaderInterface::getMessages() |
80
|
|
|
*/ |
81
|
1 |
|
public function getMessages(string $locale): array |
82
|
|
|
{ |
83
|
1 |
|
return $this->reader->getMessages($this->name, $locale); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Writes a set of messages for a specified category and locale. |
88
|
|
|
* |
89
|
|
|
* @psalm-param array<string, array<string, string>> $messages |
90
|
|
|
* |
91
|
|
|
* @param string $locale Locale to write messages for. |
92
|
|
|
* @param array $messages A set of messages to write. The format is the following: |
93
|
|
|
* ```php |
94
|
|
|
* [ |
95
|
|
|
* 'key1' => [ |
96
|
|
|
* 'message' => 'translation1', |
97
|
|
|
* // Extra metadata that writer may use: |
98
|
|
|
* 'comment' => 'Translate carefully!', |
99
|
|
|
* ], |
100
|
|
|
* 'key2' => [ |
101
|
|
|
* 'message' => 'translation2', |
102
|
|
|
* // Extra metadata that writer may use: |
103
|
|
|
* 'comment' => 'Translate carefully!', |
104
|
|
|
* ], |
105
|
|
|
* ] |
106
|
|
|
* ``` |
107
|
|
|
* |
108
|
|
|
* @throws UnwritableCategorySourceException When $write is not configured, or it's impossible to write the messages into the source. |
109
|
|
|
*/ |
110
|
2 |
|
public function write(string $locale, array $messages): void |
111
|
|
|
{ |
112
|
2 |
|
if ($this->writer === null) { |
113
|
1 |
|
throw new UnwritableCategorySourceException($this->name); |
114
|
|
|
} |
115
|
1 |
|
$this->writer->write($this->name, $locale, $messages); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Format the message given parameters and locale. |
120
|
|
|
* |
121
|
|
|
* @param string $message Message to be formatted. |
122
|
|
|
* @param array $parameters Parameters to use. |
123
|
|
|
* @param string $locale Locale to use. Usually affects formatting numbers, dates etc. |
124
|
|
|
* @param MessageFormatterInterface $defaultFormatter Message formatter that will be used if formatter not specified |
125
|
|
|
* in message category. |
126
|
|
|
* |
127
|
|
|
* @return string Formatted message. |
128
|
|
|
*/ |
129
|
56 |
|
public function format( |
130
|
|
|
string $message, |
131
|
|
|
array $parameters, |
132
|
|
|
string $locale, |
133
|
|
|
MessageFormatterInterface $defaultFormatter |
134
|
|
|
): string { |
135
|
56 |
|
return ($this->formatter ?? $defaultFormatter)->format($message, $parameters, $locale); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|