From 5a6f16b4e93e0479a936db237f29684e8969d2ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Fri, 16 Jun 2023 21:55:42 +0200 Subject: [PATCH] Set target-language to locale code or keep it if already set (#2876) * Simplify locale_code * Update iOS locales map --- pontoon/sync/formats/xliff.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pontoon/sync/formats/xliff.py b/pontoon/sync/formats/xliff.py index 00ea5fd078..93bab8f264 100644 --- a/pontoon/sync/formats/xliff.py +++ b/pontoon/sync/formats/xliff.py @@ -46,6 +46,7 @@ def __init__(self, path, locale, source_resource=None): self.locale = locale self.source_resource = source_resource self.entities = {} + self.target_language = None # Copy entities from the source_resource if it's available. if source_resource: @@ -96,6 +97,12 @@ def __init__(self, path, locale, source_resource=None): # Add the entity to the entities dictionary using its key as the dictionary key self.entities[entity.key] = entity + # Store target-language, needed for serialization + file_tag = self.xliff_file.namespaced("file") + file_nodes = self.xliff_file.document.getroot().iterchildren(file_tag) + if file_nodes: + self.target_language = next(file_nodes).get("target-language") + @property def translations(self): return sorted(self.entities.values(), key=lambda e: e.order) @@ -145,22 +152,24 @@ def save(self, locale): if target is not None and "state" in target.attrib: del target.attrib["state"] + # Map locale codes for iOS: + # http://www.ibabbleon.com/iOS-Language-Codes-ISO-639.html locale_mapping = { - "bn-IN": "bn", "ga-IE": "ga", "nb-NO": "nb", "nn-NO": "nn", + "sat": "sat-Olck", "sv-SE": "sv", + "tl": "fil", + "zgh": "tzm", } - locale_code = locale.code - if locale_code in locale_mapping: - locale_code = locale_mapping[locale_code] + + locale_code = locale_mapping.get(locale.code, locale.code) # Set target-language if not set file_node = self.xliff_file.namespaced("file") for node in self.xliff_file.document.getroot().iterchildren(file_node): - if not node.get("target-language"): - node.set("target-language", locale_code) + node.set("target-language", self.target_language or locale_code) # Serialize and save the updated XLIFF file. with open(self.path, "wb") as f: