@@ -8,8 +8,53 @@ private val special =
8
8
9
9
/* *
10
10
* Processor for transforming patterns.
11
+ *
12
+ * This class can be used to transform patterns in a way that is friendly
13
+ * to localization workflows which want to allow text characters which are
14
+ * special in Fluent to be written as regular text.
15
+ *
16
+ * According to the Fluent syntax, characters like the curly braces must be
17
+ * enclosed in StringLiterals if they are supposed to be part of the
18
+ * translation content. Otherwise, an open curly brace would start a Placeable
19
+ * and likely lead to a syntax error.
20
+ *
21
+ * Workflows in which the support for Fluent placeables is limited may choose
22
+ * to provide their own visual cues for them. This often comes in form of
23
+ * visual placeholders which can be rearranged within a translation
24
+ * segment by the translator, but whose contents cannot be modified.
25
+ *
26
+ * In these workflows, the special meaning of the character like the curly
27
+ * braces is void; the translator is not able to insert new placeables by
28
+ * opening a curly brace anyways. Thus, for translators' convenience, the
29
+ * curly brace can be treated as a regular text character and part of the
30
+ * translation content.
31
+ *
32
+ * The Processor's methods allow baking StringLiterals into TextElements, and
33
+ * then "un-baking" them again if required by the Fluent syntax. Note that
34
+ * all StringLiterals are baked, while only some are un-baked. The Processor
35
+ * is lossy.
11
36
*/
12
37
class Processor {
38
+ /* *
39
+ * "Bake" the values of StringLiterals into TextElements. This is a lossy
40
+ * transformation for literals which are not special in Fluent syntax.
41
+ *
42
+ * Examples:
43
+ *
44
+ * →Hello, {"{-_-}"}.
45
+ * ←Hello, {-_-}.
46
+ *
47
+ * →{" "}Hello, world!
48
+ * ← Hello, world!
49
+ *
50
+ * →A multiline pattern:
51
+ * {"*"} Asterisk is special
52
+ * ←A multiline pattern:
53
+ * * Asterisk is special
54
+ *
55
+ * →Copyright {"\u00A9"} 2020
56
+ * ←Copyright © 2020
57
+ */
13
58
fun unescapeLiteralsToText (pattern : Pattern ): Pattern {
14
59
val result = Pattern ()
15
60
for (elem in textFromLiterals(pattern)) {
@@ -18,6 +63,28 @@ class Processor {
18
63
return result
19
64
}
20
65
66
+ /* *
67
+ * "Un-bake" special characters into StringLiterals, which would otherwise
68
+ * cause syntax errors with Fluent parsers. Character sequences which might
69
+ * have been previously enclosed in StringLiterals, will not be un-baked,
70
+ * provided they are valid text characters in Fluent syntax.
71
+ *
72
+ * Examples:
73
+ *
74
+ * →Hello, {-_-}.
75
+ * ←Hello, {"{"}-_-{"}"}.
76
+ *
77
+ * → Hello, world!
78
+ * ←{""} Hello, world!
79
+ *
80
+ * →A multiline pattern:
81
+ * * Asterisk is special
82
+ * ←A multiline pattern:
83
+ * {"*"} Asterisk is special
84
+ *
85
+ * →Copyright © 2020
86
+ * ←Copyright © 2020
87
+ */
21
88
fun escapeTextToLiterals (pattern : Pattern ): Pattern {
22
89
val result = Pattern ()
23
90
for (elem in literalsFromText(pattern)) {
0 commit comments