-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfMessageSource_Aggregate.class.php
254 lines (227 loc) · 6 KB
/
sfMessageSource_Aggregate.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfMessageSource_Aggregate aggregates several message source objects.
*
* @package symfony
* @subpackage i18n
* @author Fabien Potencier <[email protected]>
* @version SVN: $Id$
*/
class sfMessageSource_Aggregate extends sfMessageSource
{
protected $messageSources = array();
/**
* Constructor.
*
* The order of the messages sources in the array is important.
* This class will take the first translation found in the message sources.
*
* @param array $messageSources An array of message sources.
*
* @see MessageSource::factory();
*/
function __construct($messageSources)
{
$this->messageSources = $messageSources;
}
public function setCulture($culture)
{
parent::setCulture($culture);
foreach ($this->messageSources as $messageSource)
{
$messageSource->setCulture($culture);
}
}
/**
* Gets the last modified unix-time for this particular catalogue+variant.
*
* @param string $source catalogue+variant
* @return int last modified in unix-time format.
*/
protected function getLastModified($sources)
{
$lastModified = time();
foreach ($sources as $source)
{
if (0 !== $sourceLastModified = $source[0]->getLastModified($source[1]))
{
$lastModified = min($lastModified, $sourceLastModified);
}
}
return $lastModified;
}
/**
* Determines if the source is valid.
*
* @param string $source catalogue+variant
* @return boolean true if valid, false otherwise.
*/
public function isValidSource($sources)
{
foreach ($sources as $source)
{
if (false === $source[0]->isValidSource($source[1]))
{
continue;
}
return true;
}
return false;
}
/**
* Gets the source, this could be a filename or database ID.
*
* @param string $variant catalogue+variant
* @return string the resource key
*/
public function getSource($variant)
{
$sources = array();
foreach ($this->messageSources as $messageSource)
{
$sources[] = array($messageSource, $messageSource->getSource(str_replace($messageSource->getId(), '', $variant)));
}
return $sources;
}
/**
* Loads the message for a particular catalogue+variant.
* This methods needs to implemented by subclasses.
*
* @return array of translation messages.
*/
public function &loadData($sources)
{
$messages = array();
foreach ($sources as $source)
{
if (false === $source[0]->isValidSource($source[1]))
{
continue;
}
$data = $source[0]->loadData($source[1]);
if (is_array($data))
{
$messages = array_merge($data, $messages);
}
}
return $messages;
}
/**
* Gets all the variants of a particular catalogue.
* This method must be implemented by subclasses.
*
* @param string $catalogue catalogue name
* @return array list of all variants for this catalogue.
*/
public function getCatalogueList($catalogue)
{
$variants = array();
foreach ($this->messageSources as $messageSource)
{
foreach ($messageSource->getCatalogueList($catalogue) as $variant)
{
$variants[] = $messageSource->getId().$variant;
}
}
return $variants;
}
/**
* Adds a untranslated message to the source. Need to call save()
* to save the messages to source.
*
* @param string $message message to add
*/
public function append($message)
{
// Append to the first message source only
if (count($this->messageSources))
{
$this->messageSources[0]->append($message);
}
}
/**
* Updates the translation.
*
* @param string $text the source string.
* @param string $target the new translation string.
* @param string $comments comments
* @param string $catalogue the catalogue of the translation.
* @return boolean true if translation was updated, false otherwise.
*/
public function update($text, $target, $comments, $catalogue = 'messages')
{
// Only update one message source
foreach ($this->messageSources as $messageSource)
{
if ($messageSource->update($text, $target, $comments, $catalogue))
{
return true;
}
}
return false;
}
/**
* Deletes a particular message from the specified catalogue.
*
* @param string $message the source message to delete.
* @param string $catalogue the catalogue to delete from.
* @return boolean true if deleted, false otherwise.
*/
public function delete($message, $catalogue = 'messages')
{
$retval = false;
foreach ($this->messageSources as $messageSource)
{
if ($messageSource->delete($message, $catalogue))
{
$retval = true;
}
}
return $retval;
}
/**
* Saves the list of untranslated blocks to the translation source.
* If the translation was not found, you should add those
* strings to the translation source via the <b>append()</b> method.
*
* @param string $catalogue the catalogue to add to
* @return boolean true if saved successfuly, false otherwise.
*/
public function save($catalogue = 'messages')
{
$retval = false;
foreach ($this->messageSources as $messageSource)
{
if ($messageSource->save($catalogue))
{
$retval = true;
}
}
return $retval;
}
public function getId()
{
$id = '';
foreach ($this->messageSources as $messageSource)
{
$id .= $messageSource->getId();
}
return md5($id);
}
/**
* Returns a list of catalogue as key and all it variants as value.
*
* @return array list of catalogues
*/
public function catalogues()
{
throw new sfException('The "catalogues()" method is not implemented for this message source.');
}
}