@@ -4,6 +4,7 @@ Using syntax
4
4
The ``org.projectfluent:syntax `` package provides a parser, a serializer, and libraries
5
5
for analysis and processing of Fluent files.
6
6
7
+
7
8
Parsing
8
9
-------
9
10
@@ -29,6 +30,7 @@ To create Fluent syntax from AST objects, use
29
30
serializer.serialize(resource)
30
31
serializer.serialize(resource.body[0])
31
32
33
+
32
34
Analysis (Visitor)
33
35
------------------
34
36
@@ -51,6 +53,7 @@ to continue iteration.
51
53
}
52
54
}
53
55
56
+
54
57
Custom Traversal (childrenOf)
55
58
-----------------------------
56
59
@@ -69,3 +72,89 @@ iterate over children.
69
72
listOf("default", "key", "span", "value"),
70
73
variant_props.map { (name, _) -> name }.sorted().toList()
71
74
)
75
+
76
+
77
+ Pattern Processing
78
+ ------------------
79
+
80
+ The :py:class: `syntax.processor.Processor ` class can be used to transform
81
+ patterns in a way that is friendly to localization workflows which want to
82
+ allow text characters which are special in Fluent to be written as regular
83
+ text.
84
+
85
+ According to the Fluent syntax, characters like the curly braces must be
86
+ enclosed in :py:class: `syntax.ast.StringLiteral ` instances if they are
87
+ supposed to be part of the translation content. Otherwise, an open curly
88
+ brace would start a :py:class: `syntax.ast.Placeable ` and likely lead to a
89
+ syntax error.
90
+
91
+ Workflows in which the support for Fluent placeables is limited may choose to
92
+ provide their own visual cues for them. This often comes in form of visual
93
+ placeholders which can be rearranged within a translation segment by the
94
+ translator, but whose contents cannot be modified.
95
+
96
+ In these workflows, the special meaning of the character like the curly
97
+ braces is void; the translator is not able to insert new placeables by
98
+ opening a curly brace anyways. Thus, for translators' convenience, the curly
99
+ brace can be treated as a regular text character and part of the translation
100
+ content.
101
+
102
+ The :py:class: `syntax.processor.Processor `'s methods allow baking
103
+ :py:class: `syntax.ast.StringLiteral ` instances into surrounding
104
+ :py:class: `syntax.ast.TextElement ` instances, and then "un-baking" them again
105
+ if required by the Fluent syntax. Note that all string literals are baked,
106
+ while only some are un-baked. The processing is a lossy transformation.
107
+
108
+ .. note ::
109
+ Processed patterns are not valid Fluent AST nodes anymore and must not be
110
+ serialized without first un-processing them.
111
+
112
+
113
+ Baking literals into text
114
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
115
+
116
+ Use the :py:func: `unescapeLiteralsToText ` method to bake the values of string
117
+ literals into the surrounding text elements. This is a lossy transformation
118
+ for literals which are not special in Fluent syntax.
119
+
120
+ Examples::
121
+
122
+ →Hello, {"{-_-}"}.
123
+ ←Hello, {-_-}.
124
+
125
+ →{" "}Hello, world!
126
+ ← Hello, world!
127
+
128
+ →A multiline pattern:
129
+ {"*"} Asterisk is special
130
+ ←A multiline pattern:
131
+ * Asterisk is special
132
+
133
+ →Copyright {"\u00A9"} 2020
134
+ ←Copyright © 2020
135
+
136
+
137
+ Un-baking special characters into literals
138
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139
+
140
+ Use the :py:func: `escapeTextToLiterals ` method to un-bake special characters
141
+ into string literals, which would otherwise cause syntax errors with Fluent
142
+ parsers. Character sequences which might have been previously enclosed in
143
+ string literals will not be un-baked as long as they are valid text
144
+ characters in Fluent syntax.
145
+
146
+ Examples::
147
+
148
+ →Hello, {-_-}.
149
+ ←Hello, {"{"}-_-{"}"}.
150
+
151
+ → Hello, world!
152
+ ←{""} Hello, world!
153
+
154
+ →A multiline pattern:
155
+ * Asterisk is special
156
+ ←A multiline pattern:
157
+ {"*"} Asterisk is special
158
+
159
+ →Copyright © 2020
160
+ ←Copyright © 2020
0 commit comments