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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
<?php /** * Sync/Merge utility akin to msgmerge */ class Loco_gettext_Matcher extends LocoFuzzyMatcher {
/** * Whether copying translation from source references (normally for a POT we won't) * @var bool */ private $translate;
/** * Number of translations pulled from source (when source is PO) * @var int */ private $translated;
/** * @var Loco_package_Project */ private $project;
/** * @var array[]|null */ private $hashes;
public function __construct( Loco_package_Project $project ){ $this->project = $project; }
/** * Initialize matcher with current valid source strings (ref.pot) * @param Loco_gettext_Data $pot POT reference * @param bool $translate Whether copying translations from reference data * @return int */ public function loadRefs( Loco_gettext_Data $pot, $translate = false ){ $ntotal = 0; $this->translate = (bool) $translate; $this->translated = 0; /* @var LocoPoMessage $new */ foreach( $pot as $new ){ $ntotal++; $this->add($new); } return $ntotal; }
/** * Perform a reverse lookup for a file reference from its pre-computed hash */ private function findScript( $hash ){ $map = $this->hashes; // build full index of all script hashes under configured source locations. if( is_null($map) ){ $map = []; $scripts = clone $this->project->getSourceFinder(); $scripts->filterExtensions(['js']); $basepath = $this->project->getBundle()->getDirectoryPath(); /* @var Loco_fs_File $jsfile */ foreach( $scripts->export() as $jsfile ){ $ref = $jsfile->getRelativePath($basepath); if( substr($ref,-7) === '.min.js' ) { $ref = substr($ref,0,-7).'.js'; } $map[ md5($ref) ] = $ref; } $this->hashes = $map; } return array_key_exists($hash,$map) ? $map[$hash] : ''; }
/** * Add further source strings from JSON/JED file */ private function loadJson( Loco_fs_File $file ):int { $unique = 0; $jed = json_decode( $file->getContents(), true ); if( ! is_array($jed) || ! array_key_exists('locale_data',$jed) || ! is_array($jed['locale_data']) ){ throw new Loco_error_Debug( $file->basename().' is not JED formatted'); } // without a file reference, strings will never be compiled back to the correct JSON. // if missing from JED, we'll attempt reverse match from scripts found on disk. $ref = array_key_exists('source',$jed) ? $jed['source'] : ''; if( '' === $ref || ! is_string($ref) ){ $name = $file->basename(); $ref = preg_match('/-([0-9a-f]{32})\\.json$/',$name,$r) ? $this->findScript($r[1]) : ''; if( '' === $ref ){ throw new Loco_error_Debug($name.' has no "source" key; script is unknown'); } // The hash is pre-computed and .js file is known to exist, so we'll skip filters here. // The compiler will still filter this reference, so it could potentially yield a different hash. // Loco_error_AdminNotices::debug($name.' has no "source" key; reverse matched '.$ref); } // We won't search the original script to know the line number, but this must be a valid reference // TODO We could extract the JS here, and search for each string in the JSON, but may not be 100% reliable. $ref .= ':1'; // not checking domain key. Should be valid if passed here and should only be one. foreach( $jed['locale_data'] as /*$domain =>*/ $keys ){ foreach( $keys as $msgid => $arr ){ if( '' === $msgid || ! is_array($arr) || ! isset($arr[0]) ){ continue; } $msgctxt = ''; // Unglue "msgctxt\4msgid" unique key $parts = explode("\4",$msgid,2); if( array_key_exists(1,$parts) ){ list($msgctxt,$msgid) = $parts; // TODO handle empty msgid case that uses weird "msgctxt\4(msgctxt)" format? } // string may exist in original template, and also in multiple JSONs. $new = ['source'=>$msgid,'context'=>$msgctxt,'refs'=>$ref ]; $old = $this->getArrayRef($new); if( $old ){ $refs = array_key_exists('refs',$old) ? (string) $old['refs'] : ''; if( '' === $refs ){ $old['refs'] = $ref; } else if( 0 === preg_match('/\\b'.preg_quote($ref,'/').'\\b/',$refs) ){ $old['refs'].= ' '.$ref; } $new = $old; } else { $unique++; } // Add translation from JSON only if not present in merged PO already if( $this->translate && ( ! array_key_exists('target',$new) || '' === $new['target'] ) ){ $new['target'] = $arr[0]; } $message = new LocoPoMessage($new); $this->add($message); // handle plurals, noting that msgid_plural is not stored in JED structure if( 1 < count($arr) ){ $index = 0; $plurals = $old && array_key_exists('plurals',$old) ? $old['plurals'] : []; while( array_key_exists(++$index,$arr) ){ if( array_key_exists($index,$plurals) ){ $raw = $plurals[$index]; if( $raw instanceof ArrayObject ){ $raw = $raw->getArrayCopy(); } } else { $raw = ['source'=>'','target'=>'']; } if( $this->translate && ( ! array_key_exists('target',$raw) || '' === $raw['target'] ) ){ $raw['target'] = $arr[$index]; } // use translation as missing msgid_plural only if msgid matches msgstr (English file) if( 1 === $index && '' === $raw['source'] ){ if( $arr[0] === $msgid ){ $raw['source'] = $arr[1]; } /*else { Loco_error_AdminNotices::debug('msgid_plural missing for msgid '.json_encode($msgid) ); }*/ } $plurals[$index] = new LocoPoMessage($raw); } $message['plurals'] = $plurals; } } } return $unique; }
/** * Shortcut for loading multiple jsons with error tolerance * @param Loco_fs_File[] $jsons * @return int */ public function loadJsons( array $jsons ){ $n = 0; foreach( $jsons as $jsonfile ){ try { $n += $this->loadJson($jsonfile); } catch( Loco_error_Exception $e ){ Loco_error_AdminNotices::add($e); } } return $n; }
/** * Update still-valid sources, deferring unmatched (new strings) for deferred fuzzy match * @param LocoPoIterator $original Existing definitions * @param LocoPoIterator $merged Resultant definitions * @return string[] keys matched exactly */ public function mergeValid( LocoPoIterator $original, LocoPoIterator $merged ){ $valid = []; $translate = $this->translate; /* @var LocoPoMessage $old */ foreach( $original as $old ){ $new = $this->match($old); // if existing source is still valid, merge any changes if( $new instanceof LocoPoMessage ){ $p = clone $old; $p->merge($new,$translate); $merged->push($p); $valid[] = $p->getKey(); // increment counter if translation was merged if( $translate && ! $old->translated() ){ $this->translated += $new->translated(); } } } return $valid; }
/** * Perform fuzzy matching after all exact matches have been attempted * @param LocoPoIterator $merged Resultant definitions * @return string[] strings fuzzy-matched */ public function mergeFuzzy( LocoPoIterator $merged ){ $fuzzy = []; foreach( $this->getFuzzyMatches() as $pair ){ list($old,$new) = $pair; $p = clone $old; $p->merge($new); $merged->push($p); $fuzzy[] = $p->getKey(); } return $fuzzy; }
/** * Add unmatched strings remaining as NEW source strings * @param LocoPoIterator $merged Resultant definitions to accept new strings * @return string[] strings added */ public function mergeAdded( LocoPoIterator $merged ){ $added = []; $translate = $this->translate; /* @var LocoPoMessage $new */ foreach( $this->unmatched() as $new ){ $p = clone $new; // remove translations unless configured to keep if( $p->translated() && ! $translate ){ $p->strip(); } $merged->push($p); $added[] = $p->getKey(); } return $added; }
/** * Perform full merge and return result suitable from front end. * @param LocoPoIterator $original Existing definitions * @param LocoPoIterator $merged Resultant definitions * @return array result */ public function merge( LocoPoIterator $original, LocoPoIterator $merged ){ $this->mergeValid($original,$merged); $fuzzy = $this->mergeFuzzy($merged); $added = $this->mergeAdded($merged); /* @var LocoPoMessage $old */ $dropped = []; foreach( $this->redundant() as $old ){ $dropped[] = $old->getKey(); } // return to JavaScript with stats in the same form as old front end merge return [ 'add' => $added, 'fuz' => $fuzzy, 'del' => $dropped, 'trn' => $this->translated, ]; }
/** * @param array $a * @return array */ private function getArrayRef( array $a ){ $r = $this->getRef($a); if( is_null($r) ){ return []; } if( $r instanceof ArrayObject ){ return $r->getArrayCopy(); } throw new Exception( (is_object($r)?get_class($r):gettype($r) ).' returned from '.get_class($this).'::getRef'); }
}
|