|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework, SpiralScout LLC. |
|
4
|
|
|
* |
|
5
|
|
|
* @package spiralFramework |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
* @copyright ©2009-2011 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace Spiral\Translator; |
|
10
|
|
|
|
|
11
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
12
|
|
|
use Spiral\Core\HippocampusInterface; |
|
13
|
|
|
use Spiral\Translator\Exceptions\CatalogueException; |
|
14
|
|
|
use Symfony\Component\Translation\MessageCatalogue; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Similar to Symfony catalogue, however this one does not operate with fallback locale. |
|
18
|
|
|
* |
|
19
|
|
|
* @todo improve |
|
20
|
|
|
*/ |
|
21
|
|
|
class Catalogue |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Prefix for memory sections. |
|
25
|
|
|
*/ |
|
26
|
|
|
const MEMORY_LOCATION = "translator"; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Locale name. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $locale = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Cached domains data (aggregated and loaded on domain basis). |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $domains = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var HippocampusInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $memory = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $locale |
|
49
|
|
|
* @param HippocampusInterface $memory |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($locale, HippocampusInterface $memory) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->locale = $locale; |
|
54
|
|
|
$this->memory = $memory; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getLocale() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->locale; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Locale domains. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getDomains() |
|
71
|
|
|
{ |
|
72
|
|
|
$domains = array_keys($this->domains); |
|
73
|
|
|
foreach ($this->memory->getSections(static::MEMORY_LOCATION) as $section) { |
|
74
|
|
|
if (strpos($section, "{$this->locale}-") === 0) { |
|
75
|
|
|
$domains[] = substr($section, strlen("{$this->locale}-")); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return array_unique($domains); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Check if domain message exists. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $domain |
|
86
|
|
|
* @param string $string |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function has($domain, $string) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->loadDomain($domain); |
|
92
|
|
|
|
|
93
|
|
|
return array_key_exists($string, $this->domains[$domain]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get domain message. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $domain |
|
100
|
|
|
* @param string $string |
|
101
|
|
|
* @return string |
|
102
|
|
|
* @throws CatalogueException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function get($domain, $string) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$this->has($domain, $string)) { |
|
107
|
|
|
throw new CatalogueException("Undefined string in domain {$domain}."); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->domains[$domain][$string]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get all domain messages. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $domain |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getMessages($domain) |
|
120
|
|
|
{ |
|
121
|
|
|
if (!isset($this->domains[$domain])) { |
|
122
|
|
|
$this->loadDomain($domain); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->domains[$domain]; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Adding string association to be stored into memory. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $domain |
|
132
|
|
|
* @param string $string |
|
133
|
|
|
* @param string $value |
|
134
|
|
|
*/ |
|
135
|
|
|
public function set($domain, $string, $value) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->domains[$domain][$string] = $value; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param MessageCatalogue $catalogue |
|
142
|
|
|
* @param bool $follow When set to true messages from given catalogue will overwrite |
|
143
|
|
|
* existed messages. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function mergeFrom(MessageCatalogue $catalogue, $follow = true) |
|
146
|
|
|
{ |
|
147
|
|
|
foreach ($catalogue->all() as $domain => $messages) { |
|
148
|
|
|
if (!isset($this->domains[$domain])) { |
|
149
|
|
|
$this->domains[$domain] = []; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($follow) { |
|
153
|
|
|
//MessageCatalogue string has higher priority that string stored in memory |
|
154
|
|
|
$this->domains[$domain] = array_merge($messages, $this->domains[$domain]); |
|
155
|
|
|
} else { |
|
156
|
|
|
$this->domains[$domain] = array_merge($this->domains[$domain], $messages); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Converts into one MessageCatalogue. |
|
163
|
|
|
* |
|
164
|
|
|
* @return MessageCatalogue |
|
165
|
|
|
*/ |
|
166
|
|
|
public function toMessageCatalogue() |
|
167
|
|
|
{ |
|
168
|
|
|
return new MessageCatalogue($this->locale, $this->domains); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Load all catalogue domains. |
|
173
|
|
|
* |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function loadDomains() |
|
177
|
|
|
{ |
|
178
|
|
|
foreach ($this->getDomains() as $domain) { |
|
179
|
|
|
$this->loadDomain($domain); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Save catalogue into memory. |
|
187
|
|
|
*/ |
|
188
|
|
|
public function saveDomains() |
|
189
|
|
|
{ |
|
190
|
|
|
foreach ($this->domains as $domain => $data) { |
|
191
|
|
|
$this->saveDomain($domain, $data); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Trying to load domain data from memory. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $domain |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function loadDomain($domain) |
|
201
|
|
|
{ |
|
202
|
|
|
$data = $this->memory->loadData("{$this->locale}-{$domain}", static::MEMORY_LOCATION); |
|
203
|
|
|
|
|
204
|
|
|
if (empty($data)) { |
|
205
|
|
|
$data = []; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$this->domains[$domain] = $data; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Save domain data into memory. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $domain |
|
215
|
|
|
* @param array $data |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function saveDomain($domain, $data) |
|
218
|
|
|
{ |
|
219
|
|
|
$this->memory->saveData("{$this->locale}-{$domain}", $data, static::MEMORY_LOCATION); |
|
220
|
|
|
} |
|
221
|
|
|
} |