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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
"""Migration Transforms.
Transforms are AST nodes which describe how legacy translations should be
migrated. They are created inert and only return the migrated AST nodes when
they are evaluated by a MigrationContext.
All Transforms evaluate to Fluent Patterns. This makes them suitable for
defining migrations of values of message, attributes and variants. The special
CONCAT Transform is capable of joining multiple Patterns returned by evaluating
other Transforms into a single Pattern. It can also concatenate Pattern
elements: TextElements and Placeables.
The COPY, REPLACE and PLURALS Transforms inherit from Source which is a special
AST Node defining the location (the file path and the id) of the legacy
translation. During the migration, the current MigrationContext scans the
migration spec for Source nodes and extracts the information about all legacy
translations being migrated. For instance,
COPY('file.dtd', 'hello')
is equivalent to:
FTL.Pattern([
Source('file.dtd', 'hello')
])
Sometimes it's useful to work with text rather than (path, key) source
definitions. This is the case when the migrated translation requires some
hardcoded text, e.g. <a> and </a> when multiple translations become a single
one with a DOM overlay. In such cases it's best to use FTL.TextElements:
FTL.Message(
id=FTL.Identifier('update-failed'),
value=CONCAT(
COPY('aboutDialog.dtd', 'update.failed.start'),
FTL.TextElement('<a>'),
COPY('aboutDialog.dtd', 'update.failed.linkText'),
FTL.TextElement('</a>'),
COPY('aboutDialog.dtd', 'update.failed.end'),
)
)
The REPLACE_IN_TEXT Transform also takes TextElements as input, making it
possible to pass it as the foreach function of the PLURALS Transform. In the
example below, each slice of the plural string is converted into a
TextElement by PLURALS and then run through the REPLACE_IN_TEXT transform.
FTL.Message(
FTL.Identifier('delete-all'),
value=PLURALS(
'aboutDownloads.dtd',
'deleteAll',
VARIABLE_REFERENCE('num'),
lambda text: REPLACE_IN_TEXT(
text,
{
'#1': VARIABLE_REFERENCE('num')
}
)
)
)
"""
"""Flatten a list of FTL nodes into an iterator over PatternElements."""
# PY3 yield from element.elements
yield from
yield
yield
=
=
"""Extract leading or trailing whitespace from a TextElement.
Return a tuple of (Placeable, TextElement) in which the Placeable
encodes the extracted whitespace as a StringLiteral and the
TextElement has the same amount of whitespace removed. The
Placeable with the extracted whitespace is always returned first.
If the element starts or ends with a newline, add an empty
StringLiteral.
"""
=
# If white-space is None, we're a newline. Add an
# empty { "" }
= or
=
return , None
# Either text or block_text matched the rest.
= or
return ,
return None,
=
# Normalize text content: convert text content to TextElements, join
# adjacent text and prune empty. Text content is either existing
# TextElements or whitespace-only StringLiterals. This may result in
# leading and trailing whitespace being put back into TextElements if
# the new Pattern is built from existing Patterns (CONCAT(COPY...)).
# The leading and trailing whitespace of the new Pattern will be
# extracted later into new StringLiterals.
=
=
# The element does not contain text content which should be
# normalized. It may be a number, a reference, or
# a StringLiteral which should be preserved in the Pattern.
continue
=
# Join adjacent TextElements.
+=
# Normalize non-empty text to a TextElement.
# Prune empty text.
pass
# Store empty values explicitly as {""}.
=
return
# Extract explicit leading whitespace into a StringLiteral.
, =
=
# Extract explicit trailing whitespace into a StringLiteral.
, =
=
return
"""Base class for Transforms that get translations from source files.
The contract is that the first argument is the source path, and the
second is a key representing legacy string IDs, or Fluent id.attr.
"""
=
=
"""Declare a Fluent source translation to be copied over.
When evaluated, it clones the Pattern of the parsed source.
"""
=
return
"""Create a Pattern with the translation value from the given source.
The given key can be a Message ID, Message ID.attribute_name, or
Term ID. Accessing Term attributes is not supported, as they're internal
to the localization.
"""
pass
"""Base class for modifying a Fluent pattern as part of a migration.
Implement visit_* methods of the Transformer pattern to do the
actual modifications.
"""
=
return
# Make sure we're creating valid Patterns after restructuring
# transforms.
=
=
return
# Ensure we have a Placeable with an expression still.
# Transforms could have replaced the expression with
# a Pattern or PatternElement, in which case we
# just pass that through.
# Patterns then get flattened by visit_Pattern.
=
return
return
"""Declare the source translation to be migrated with other transforms.
When evaluated, `Source` returns a TextElement with the content from the
source translation. Escaped characters are unescaped by the
compare-locales parser according to the file format:
- in properties files: \\uXXXX,
- in DTD files: known named, decimal, and hexadecimal HTML entities.
Consult the following files for the list of known named HTML entities:
https://github.com/python/cpython/blob/2.7/Lib/htmlentitydefs.py
https://github.com/python/cpython/blob/3.6/Lib/html/entities.py
By default, leading and trailing whitespace on each line as well as
leading and trailing empty lines will be stripped from the source
translation's content. Set `trim=False` to disable this behavior.
"""
=
return
# strip leading white-space from each line
=
# strip trailing white-space from each line
=
# strip leading and trailing empty lines
=
return
=
=
return
"""Create a Pattern with the translation value from the given source."""
=
return
=
= 1
yield
+= 1
"""Normalize printf arguments so that they're all numbered.
Gecko forbids mixing unnumbered and numbered ones, so
we just need to convert unnumbered to numbered ones.
Also remove ones that have zero width, as they're intended
to be removed from the output by the localizer.
"""
=
return
= ==
return
=
return
return
"""Create a Pattern from a TextElement and replace legacy placeables.
The original placeables are defined as keys on the `replacements` dict.
For each key the value must be defined as a FTL Pattern, Placeable,
TextElement or Expression to be interpolated.
"""
=
=
=
# For each specified replacement, find all indices of the original
# placeable in the source translation. If missing, the list of indices
# will be empty.
=
=
=
# Build a dict of indices to replacement keys.
=
=
# Order the replacements by the position of the original placeable in
# the translation.
=
# A list of PatternElements built from the legacy translation and the
# FTL replacements. It may contain empty or adjacent TextElements.
=
=
# Convert original placeables and text into FTL Nodes. For each
# original placeable the translation will be partitioned around it and
# the text before it will be converted into an `FTL.TextElement` and
# the placeable will be replaced with its replacement.
, , =
# Don't forget about the tail after the loop ends.
return
"""Create a Pattern with interpolations from given source.
Interpolations in the translation value from the given source will be
replaced with FTL placeables using the `REPLACE_IN_TEXT` transform.
"""
# We default normalize_printf to False except for .properties files.
# We still allow the caller to override the default value.
= False
=
del
= True
=
=
=
return
"""Create a Pattern with plurals from given source.
Build an `FTL.SelectExpression` with the supplied `selector` and variants
extracted from the source. The original translation should be a
semicolon-separated list of plural forms. Each form will be converted
into a TextElement and run through the `foreach` function, which should
return an `FTL.Node` or a `Transform`. By default, the `foreach` function
creates a valid Pattern from the TextElement passed into it.
"""
=
=
=
=
=
=
=
# The default CLDR form should be the last we have in DEFAULT_ORDER,
# usually `other`, but in some cases `many`. If we don't have a variant
# for that, we'll append one, using the, in CLDR order, last existing
# variant in the legacy translation. That may or may not be the last
# variant.
=
# Match keys to legacy forms in the order they are defined in Gecko's
# PluralForm.jsm. Filter out empty forms.
=
# A special case for legacy translations which don't define any
# plural forms.
return
# A special case for languages with one plural category or one legacy
# variant. We don't need to insert a SelectExpression for them.
, =
=
return
# Make sure the default key is defined. If it's missing, use the last
# form (in CLDR order) found in the legacy translation.
, =
# Run the legacy plural form through `foreach` which returns an
# `FTL.Node` describing the transformation required for each
# variant. Then evaluate it to a migrated FTL node.
=
return
=
return
"""Create a new Pattern from Patterns, PatternElements and Expressions.
When called with at least two elements, `CONCAT` disables the trimming
behavior of the elements which are subclasses of `LegacySource` by
setting `trim=False`, unless `trim` has already been set explicitly. The
following two `CONCAT` calls are equivalent:
CONCAT(
FTL.TextElement("Hello"),
COPY("file.properties", "hello")
)
CONCAT(
FTL.TextElement("Hello"),
COPY("file.properties", "hello", trim=False)
)
Set `trim=True` explicitly to force trimming:
CONCAT(
FTL.TextElement("Hello "),
COPY("file.properties", "hello", trim=True)
)
When called with a single element and when the element is a subclass of
`LegacySource`, the trimming behavior is not changed. The following two
transforms are equivalent:
CONCAT(COPY("file.properties", "hello"))
COPY("file.properties", "hello")
"""
# We want to support both passing elements as *elements in the
# migration specs and as elements=[]. The latter is used by
# FTL.BaseNode.traverse when it recreates the traversed node using its
# attributes as kwargs.
=
# We want to make CONCAT(COPY()) equivalent to COPY() so that it's
# always safe (no-op) to wrap transforms in a CONCAT. This is used by
# the implementation of transforms_from.
# Only change trim if it hasn't been set explicitly.
= False
return