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