diff --git a/.github/scripts/clean_xliff_targets.py b/.github/scripts/clean_xliff_targets.py index 6615a7f4f..7c567558c 100644 --- a/.github/scripts/clean_xliff_targets.py +++ b/.github/scripts/clean_xliff_targets.py @@ -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 @@ -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__": diff --git a/.github/scripts/extract_source_strings.py b/.github/scripts/extract_source_strings.py index 5f7b59520..b04d2b263 100644 --- a/.github/scripts/extract_source_strings.py +++ b/.github/scripts/extract_source_strings.py @@ -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 @@ -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__": diff --git a/.github/scripts/functions.py b/.github/scripts/functions.py new file mode 100644 index 000000000..6a365f8da --- /dev/null +++ b/.github/scripts/functions.py @@ -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 = ( + '\n' + xliff_content.decode("utf-8") + ) + fp.write(xliff_content) diff --git a/.github/scripts/set_target_language_en.py b/.github/scripts/set_target_language_en.py index 6e7eb7c1f..7c508055b 100644 --- a/.github/scripts/set_target_language_en.py +++ b/.github/scripts/set_target_language_en.py @@ -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 @@ -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__": diff --git a/.github/scripts/update_other_locales.py b/.github/scripts/update_other_locales.py index bcdf26704..aca6938be 100644 --- a/.github/scripts/update_other_locales.py +++ b/.github/scripts/update_other_locales.py @@ -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 @@ -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: diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..0d20b6487 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc