summaryrefslogtreecommitdiffstats
path: root/python/l10n/convert_xul_to_fluent/lib/migration.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /python/l10n/convert_xul_to_fluent/lib/migration.py
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'python/l10n/convert_xul_to_fluent/lib/migration.py')
-rw-r--r--python/l10n/convert_xul_to_fluent/lib/migration.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/python/l10n/convert_xul_to_fluent/lib/migration.py b/python/l10n/convert_xul_to_fluent/lib/migration.py
new file mode 100644
index 0000000000..efe32c5178
--- /dev/null
+++ b/python/l10n/convert_xul_to_fluent/lib/migration.py
@@ -0,0 +1,77 @@
+from __future__ import absolute_import
+
+
+def to_chrome_path(path):
+ path = path.replace("/locales/en-US", "")
+ if path.startswith("./"):
+ path = path[2:]
+ return path
+
+
+def get_dtd_path(name, dtds):
+ return dtds[name[1:-1]]["file"]
+
+
+def get_entity_name(s):
+ return s[1:-1]
+
+
+def ind(n=0):
+ return " " * 4 * n
+
+
+def add_copy(dtd, entity_id):
+ result = "{ " + 'COPY("{0}", "{1}")'.format(dtd, entity_id) + " }\n"
+ return result
+
+
+def make_header():
+ res = "# coding=utf8\n\n"
+ res += "# Any copyright is dedicated to the Public Domain.\n"
+ res += "# http://creativecommons.org/publicdomain/zero/1.0/\n\n"
+ res += "from __future__ import absolute_import\n"
+ res += "import fluent.syntax.ast as FTL\n"
+ res += "from fluent.migrate.helpers import transforms_from\n"
+ res += "from fluent.migrate.helpers import MESSAGE_REFERENCE, "
+ res += "TERM_REFERENCE, VARIABLE_REFERENCE\n"
+ res += "from fluent.migrate import COPY, CONCAT, REPLACE\n"
+
+ return res
+
+
+def build_migration(messages, dtds, data):
+ res = make_header()
+ desc = "Bug {0} - {1}, part {{index}}.".format(data["bug_id"], data["description"])
+ res += '\n\ndef migrate(ctx):\n """{0}"""\n\n'.format(desc)
+
+ for dtd_path in data["dtd"]:
+ res += "{0}ctx.maybe_add_localization('{1}')\n".format(
+ ind(1), to_chrome_path(dtd_path)
+ )
+
+ res += "\n"
+ res += ind(1) + "ctx.add_transforms(\n"
+ res += ind(2) + "'{0}',\n".format(to_chrome_path(data["ftl"]))
+ res += ind(2) + "'{0}',\n".format(to_chrome_path(data["ftl"]))
+ res += ind(2) + "transforms_from(\n"
+ res += '"""\n'
+ for l10n_id in messages:
+ msg = messages[l10n_id]
+ if not msg["attrs"]:
+ entity = get_entity_name(msg["value"])
+ entity_path = to_chrome_path(get_dtd_path(msg["value"], dtds))
+ res += "{0} = {1}".format(l10n_id, add_copy(entity_path, entity))
+ else:
+ res += "{0} = \n".format(l10n_id)
+ for name in msg["attrs"]:
+ attr = msg["attrs"][name]
+ attr_name = get_entity_name(attr)
+ entity_path = to_chrome_path(get_dtd_path(attr, dtds))
+ res += "{0}.{1} = {2}".format(
+ ind(1), name, add_copy(entity_path, attr_name)
+ )
+ res += '"""\n'
+ res += ind(2) + ")\n"
+ res += ind(1) + ")"
+
+ return res