diff options
Diffstat (limited to 'python/l10n/convert_xul_to_fluent/lib/fluent.py')
-rw-r--r-- | python/l10n/convert_xul_to_fluent/lib/fluent.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/python/l10n/convert_xul_to_fluent/lib/fluent.py b/python/l10n/convert_xul_to_fluent/lib/fluent.py new file mode 100644 index 0000000000..ecab538579 --- /dev/null +++ b/python/l10n/convert_xul_to_fluent/lib/fluent.py @@ -0,0 +1,34 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +from __future__ import absolute_import +from fluent.syntax import ast +from fluent.syntax.serializer import FluentSerializer + + +def get_value_from_dtd(name, dtd): + return dtd[name[1:-1]]["value"] + + +def build_ftl(messages, dtd, data): + res = ast.Resource() + + for id_str in messages: + msg = messages[id_str] + l10n_id = ast.Identifier(id_str) + val = None + attrs = [] + if msg["value"]: + dtd_val = get_value_from_dtd(msg["value"], dtd) + val = ast.Pattern([ast.TextElement(dtd_val)]) + for attr_name in msg["attrs"]: + dtd_val = get_value_from_dtd(msg["attrs"][attr_name], dtd) + attr_val = ast.Pattern([ast.TextElement(dtd_val)]) + attrs.append(ast.Attribute(ast.Identifier(attr_name), attr_val)) + + m = ast.Message(l10n_id, val, attrs) + res.body.append(m) + + serializer = FluentSerializer() + return serializer.serialize(res) |