Skip to content

Commit

Permalink
Refactor code and fix conflict with XML declaration (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
flodolo authored Dec 17, 2024
1 parent 2f888df commit 610b7e2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 deletions.
9 changes: 2 additions & 7 deletions .github/scripts/clean_xliff_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# This script must be executed at the root of the repository.

from functions import write_xliff
from lxml import etree, objectify
import argparse

Expand Down Expand Up @@ -49,13 +50,7 @@ def main():
sort_children(f, "id")

# Replace the existing local file with the new XML content
with open(xliff_file, "w") as fp:
# Fix indentation of XML file
etree.indent(root)
xliff_content = etree.tostring(
tree, encoding="UTF-8", xml_declaration=True, pretty_print=True
)
fp.write(xliff_content.decode("utf-8"))
write_xliff(root, xliff_file)


if __name__ == "__main__":
Expand Down
9 changes: 2 additions & 7 deletions .github/scripts/extract_source_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# This script must be executed at the root of the repository.

from functions import write_xliff
from lxml import etree, objectify
import argparse
import os
Expand Down Expand Up @@ -116,13 +117,7 @@ def main():
sort_children(f, "id")

# Replace the existing local file with the new XML content
with open(output_xliff_file, "w") as fp:
# Fix indentation of XML file
etree.indent(root)
xliff_content = etree.tostring(
tree, encoding="UTF-8", xml_declaration=True, pretty_print=True
)
fp.write(xliff_content.decode("utf-8"))
write_xliff(root, output_xliff_file)


if __name__ == "__main__":
Expand Down
23 changes: 23 additions & 0 deletions .github/scripts/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from lxml import etree


def write_xliff(root, filename):
with open(filename, "w+") as fp:
# Fix identation of XML file
etree.indent(root)
"""
Hack to avoid conflicts with Pontoon, which uses single quotes
for the XML declaration:
1. Exclude the XML declaration when using etree.tostring()
2. Manually add the declaration with double quotes
"""
xliff_content = etree.tostring(
root,
encoding="UTF-8",
xml_declaration=False,
pretty_print=True,
)
xliff_content = (
'<?xml version="1.0" encoding="UTF-8"?>\n' + xliff_content.decode("utf-8")
)
fp.write(xliff_content)
9 changes: 2 additions & 7 deletions .github/scripts/set_target_language_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# 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 functions import write_xliff
from glob import glob
from lxml import etree
import argparse
Expand All @@ -25,13 +26,7 @@ def main():
for file_node in root.xpath("//x:file", namespaces=NS):
file_node.set("target-language", "en-US")

with open(xliff_path, "w") as fp:
# Fix indentation of XML file
etree.indent(root)
xliff_content = etree.tostring(
tree, encoding="UTF-8", xml_declaration=True, pretty_print=True
)
fp.write(xliff_content.decode("utf-8"))
write_xliff(root, xliff_path)


if __name__ == "__main__":
Expand Down
12 changes: 2 additions & 10 deletions .github/scripts/update_other_locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from argparse import RawTextHelpFormatter
from copy import deepcopy
from functions import write_xliff
from glob import glob
from lxml import etree
import argparse
Expand Down Expand Up @@ -219,16 +220,7 @@ def main():

# Replace the existing locale file with the new XML content,
# or create a new one if it's missing.
with open(l10n_file, "w+") as fp:
# Fix identation of XML file
etree.indent(reference_root_copy)
xliff_content = etree.tostring(
reference_tree_copy,
encoding="UTF-8",
xml_declaration=True,
pretty_print=True,
)
fp.write(xliff_content.decode("utf-8"))
write_xliff(reference_root_copy, l10n_file)
updated_files += 1

if updated_files == 0:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc

0 comments on commit 610b7e2

Please sign in to comment.