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
|
<?php
namespace Gettext;
/** * Static class with merge contants. */ class Merge { const ADD = 1; const REMOVE = 2;
const HEADERS_ADD = 4; const HEADERS_REMOVE = 8; const HEADERS_OVERRIDE = 16;
const LANGUAGE_OVERRIDE = 32; const DOMAIN_OVERRIDE = 64; const TRANSLATION_OVERRIDE = 128;
const COMMENTS_OURS = 256; const COMMENTS_THEIRS = 512;
const EXTRACTED_COMMENTS_OURS = 1024; const EXTRACTED_COMMENTS_THEIRS = 2048;
const FLAGS_OURS = 4096; const FLAGS_THEIRS = 8192;
const REFERENCES_OURS = 16384; const REFERENCES_THEIRS = 32768;
const DEFAULTS = 5; //1 + 4
/** * Merge the flags of two translations. * * @param Translation $from * @param Translation $to * @param int $options */ public static function mergeFlags(Translation $from, Translation $to, $options = self::DEFAULTS) { if ($options & self::FLAGS_THEIRS) { $to->deleteFlags(); }
if (!($options & self::FLAGS_OURS)) { foreach ($from->getFlags() as $flag) { $to->addFlag($flag); } } }
/** * Merge the extracted comments of two translations. * * @param Translation $from * @param Translation $to * @param int $options */ public static function mergeExtractedComments(Translation $from, Translation $to, $options = self::DEFAULTS) { if ($options & self::EXTRACTED_COMMENTS_THEIRS) { $to->deleteExtractedComments(); }
if (!($options & self::EXTRACTED_COMMENTS_OURS)) { foreach ($from->getExtractedComments() as $comment) { $to->addExtractedComment($comment); } } }
/** * Merge the comments of two translations. * * @param Translation $from * @param Translation $to * @param int $options */ public static function mergeComments(Translation $from, Translation $to, $options = self::DEFAULTS) { if ($options & self::COMMENTS_THEIRS) { $to->deleteComments(); }
if (!($options & self::COMMENTS_OURS)) { foreach ($from->getComments() as $comment) { $to->addComment($comment); } } }
/** * Merge the references of two translations. * * @param Translation $from * @param Translation $to * @param int $options */ public static function mergeReferences(Translation $from, Translation $to, $options = self::DEFAULTS) { if ($options & self::REFERENCES_THEIRS) { $to->deleteReferences(); }
if (!($options & self::REFERENCES_OURS)) { foreach ($from->getReferences() as $reference) { $to->addReference($reference[0], $reference[1]); } } }
/** * Merge the translations of two translations. * * @param Translation $from * @param Translation $to * @param int $options */ public static function mergeTranslation(Translation $from, Translation $to, $options = self::DEFAULTS) { $override = (boolean) ($options & self::TRANSLATION_OVERRIDE);
if (!$to->hasTranslation() || ($from->hasTranslation() && $override)) { $to->setTranslation($from->getTranslation()); }
if (!$to->hasPlural() || ($from->hasPlural() && $override)) { $to->setPlural($from->getPlural()); }
if (!$to->hasPluralTranslations() || ($from->hasPluralTranslations() && $override)) { $to->setPluralTranslations($from->getPluralTranslations()); } }
/** * Merge the translations of two translations. * * @param Translations $from * @param Translations $to * @param int $options */ public static function mergeTranslations(Translations $from, Translations $to, $options = self::DEFAULTS) { if ($options & self::REMOVE) { $filtered = [];
foreach ($to as $entry) { if ($from->find($entry)) { $filtered[$entry->getId()] = $entry; } }
$to->exchangeArray($filtered); }
foreach ($from as $entry) { if (($existing = $to->find($entry))) { $existing->mergeWith($entry, $options); } elseif ($options & self::ADD) { $to[] = $entry->getClone(); } } }
/** * Merge the headers of two translations. * * @param Translations $from * @param Translations $to * @param int $options */ public static function mergeHeaders(Translations $from, Translations $to, $options = self::DEFAULTS) { if ($options & self::HEADERS_REMOVE) { foreach (array_keys($to->getHeaders()) as $name) { if ($from->getHeader($name) === null) { $to->deleteHeader($name); } } }
foreach ($from->getHeaders() as $name => $value) { $current = $to->getHeader($name);
if (empty($current)) { if ($options & self::HEADERS_ADD) { $to->setHeader($name, $value); } continue; }
if (empty($value)) { continue; }
switch ($name) { case Translations::HEADER_LANGUAGE: case Translations::HEADER_PLURAL: if ($options & self::LANGUAGE_OVERRIDE) { $to->setHeader($name, $value); } break;
case Translations::HEADER_DOMAIN: if ($options & self::DOMAIN_OVERRIDE) { $to->setHeader($name, $value); } break;
default: if ($options & self::HEADERS_OVERRIDE) { $to->setHeader($name, $value); } } } } }
|