|
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
|
58 |
|
public function __construct( |
|
22
|
|
|
string $name, |
|
23
|
|
|
private MessageReaderInterface $reader, |
|
24
|
|
|
private ?MessageFormatterInterface $formatter = null |
|
25
|
|
|
) { |
|
26
|
58 |
|
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
|
57 |
|
$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
|
|
|
public function getMessages(string $locale): array |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->reader->getMessages($this->name, $locale); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Format the message given parameters and locale. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $message Message to be formatted. |
|
85
|
|
|
* @param array $parameters Parameters to use. |
|
86
|
|
|
* @param string $locale Locale to use. Usually affects formatting numbers, dates etc. |
|
87
|
|
|
* @param MessageFormatterInterface $defaultFormatter Message formatter that will be used if formatter not specified |
|
88
|
|
|
* in message category. |
|
89
|
|
|
* |
|
90
|
|
|
* @return string Formatted message. |
|
91
|
|
|
*/ |
|
92
|
55 |
|
public function format( |
|
93
|
|
|
string $message, |
|
94
|
|
|
array $parameters, |
|
95
|
|
|
string $locale, |
|
96
|
|
|
MessageFormatterInterface $defaultFormatter |
|
97
|
|
|
): string { |
|
98
|
55 |
|
return ($this->formatter ?? $defaultFormatter)->format($message, $parameters, $locale); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|