|
8 | 8 | All Transforms evaluate to Fluent Patterns. This makes them suitable for |
9 | 9 | defining migrations of values of message, attributes and variants. The special |
10 | 10 | CONCAT Transform is capable of joining multiple Patterns returned by evaluating |
11 | | -other Transforms into a single Pattern. It can also concatenate Fluent |
12 | | -Expressions, like MessageReferences and ExternalArguments. |
| 11 | +other Transforms into a single Pattern. It can also concatenate Pattern |
| 12 | +elements: TextElements and Placeables. |
13 | 13 |
|
14 | 14 | The COPY, REPLACE and PLURALS Transforms inherit from Source which is a special |
15 | 15 | AST Node defining the location (the file path and the id) of the legacy |
16 | 16 | translation. During the migration, the current MergeContext scans the |
17 | 17 | migration spec for Source nodes and extracts the information about all legacy |
18 | | -translations being migrated. Thus, |
| 18 | +translations being migrated. For instance, |
19 | 19 |
|
20 | 20 | COPY('file.dtd', 'hello') |
21 | 21 |
|
22 | 22 | is equivalent to: |
23 | 23 |
|
24 | | - LITERAL(Source('file.dtd', 'hello')) |
| 24 | + FTL.Pattern([ |
| 25 | + FTL.TextElement(Source('file.dtd', 'hello')) |
| 26 | + ]) |
25 | 27 |
|
26 | | -where LITERAL is a helper defined in the helpers.py module for creating Fluent |
27 | | -Patterns from the text passed as the argument. |
28 | | -
|
29 | | -The LITERAL helper and the special REPLACE_IN_TEXT Transforms are useful for |
30 | | -working with text rather than (path, key) source definitions. This is the case |
31 | | -when the migrated translation requires some hardcoded text, e.g. <a> and </a> |
32 | | -when multiple translations become a single one with a DOM overlay. |
| 28 | +Sometimes it's useful to work with text rather than (path, key) source |
| 29 | +definitions. This is the case when the migrated translation requires some |
| 30 | +hardcoded text, e.g. <a> and </a> when multiple translations become a single |
| 31 | +one with a DOM overlay. In such cases it's best to use the AST nodes: |
33 | 32 |
|
34 | 33 | FTL.Message( |
35 | 34 | id=FTL.Identifier('update-failed'), |
36 | 35 | value=CONCAT( |
37 | 36 | COPY('aboutDialog.dtd', 'update.failed.start'), |
38 | | - LITERAL('<a>'), |
| 37 | + FTL.TextElement('<a>'), |
39 | 38 | COPY('aboutDialog.dtd', 'update.failed.linkText'), |
40 | | - LITERAL('</a>'), |
| 39 | + FTL.TextElement('</a>'), |
41 | 40 | COPY('aboutDialog.dtd', 'update.failed.end'), |
42 | 41 | ) |
43 | 42 | ) |
44 | 43 |
|
45 | 44 | The REPLACE_IN_TEXT Transform also takes text as input, making in possible to |
46 | 45 | pass it as the foreach function of the PLURALS Transform. In this case, each |
47 | 46 | slice of the plural string will be run through a REPLACE_IN_TEXT operation. |
48 | | -Those slices are strings, so a REPLACE(path, key, …) isn't suitable for them. |
| 47 | +Those slices are strings, so a REPLACE(path, key, …) wouldn't be suitable for |
| 48 | +them. |
49 | 49 |
|
50 | 50 | FTL.Message( |
51 | 51 | FTL.Identifier('delete-all'), |
|
66 | 66 | from __future__ import unicode_literals |
67 | 67 |
|
68 | 68 | import fluent.syntax.ast as FTL |
69 | | -from .helpers import LITERAL |
| 69 | + |
| 70 | + |
| 71 | +def pattern_from_text(value): |
| 72 | + return FTL.Pattern([ |
| 73 | + FTL.TextElement(value) |
| 74 | + ]) |
70 | 75 |
|
71 | 76 |
|
72 | 77 | def evaluate(ctx, node): |
@@ -120,7 +125,7 @@ class COPY(Source): |
120 | 125 |
|
121 | 126 | def __call__(self, ctx): |
122 | 127 | source = super(self.__class__, self).__call__(ctx) |
123 | | - return LITERAL(source) |
| 128 | + return pattern_from_text(source) |
124 | 129 |
|
125 | 130 |
|
126 | 131 | class REPLACE_IN_TEXT(Transform): |
@@ -210,10 +215,12 @@ class PLURALS(Source): |
210 | 215 | Build an `FTL.SelectExpression` with the supplied `selector` and variants |
211 | 216 | extracted from the source. The source needs to be a semicolon-separated |
212 | 217 | list of variants. Each variant will be run through the `foreach` function, |
213 | | - which should return an `FTL.Node` or a `Transform`. |
| 218 | + which should return an `FTL.Node` or a `Transform`. By default, the |
| 219 | + `foreach` function transforms the source text into a Pattern with a single |
| 220 | + TextElement. |
214 | 221 | """ |
215 | 222 |
|
216 | | - def __init__(self, path, key, selector, foreach=LITERAL): |
| 223 | + def __init__(self, path, key, selector, foreach=pattern_from_text): |
217 | 224 | super(self.__class__, self).__init__(path, key) |
218 | 225 | self.selector = selector |
219 | 226 | self.foreach = foreach |
@@ -264,7 +271,7 @@ def concat_elements(acc, cur): |
264 | 271 | return acc |
265 | 272 |
|
266 | 273 | raise RuntimeError( |
267 | | - 'CONCAT accepts FTL Patterns and Expressions.' |
| 274 | + 'CONCAT accepts FTL Patterns, TextElements and Placeables.' |
268 | 275 | ) |
269 | 276 |
|
270 | 277 | # Merge adjecent `FTL.TextElement` nodes. |
|
0 commit comments