From cef19a8b130a78c4cc91c8747811433396719954 Mon Sep 17 00:00:00 2001 From: Automatic Release Builder Date: Sun, 21 Jun 2020 19:25:18 +0100 Subject: [PATCH] Catalog I18N support Read I18N satrings form aircraft data and add to the catalog --- catalog/testData/Aircraft/f16/f16a-set.xml | 11 +++++ catalog/testData/Aircraft/f16/f16b-set.xml | 10 +++++ catalog/test_catalog.py | 12 ++++- .../meta/aircraft_catalogs/catalog.py | 44 ++++++++++++++++++- 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/catalog/testData/Aircraft/f16/f16a-set.xml b/catalog/testData/Aircraft/f16/f16a-set.xml index 7dc1e21..f3377c0 100644 --- a/catalog/testData/Aircraft/f16/f16a-set.xml +++ b/catalog/testData/Aircraft/f16/f16a-set.xml @@ -7,6 +7,17 @@ true + + + + Describe the F16-A in German + + + + Describe the F16-A in French + + + 3 1 diff --git a/catalog/testData/Aircraft/f16/f16b-set.xml b/catalog/testData/Aircraft/f16/f16b-set.xml index 3379f86..ca23b6a 100644 --- a/catalog/testData/Aircraft/f16/f16b-set.xml +++ b/catalog/testData/Aircraft/f16/f16b-set.xml @@ -6,6 +6,16 @@ The F16-B is an upgraded version of the F16A. f16a + + + Describe the F16-B in German + + + + Describe the F16-B in French + + + James T Kirk diff --git a/catalog/test_catalog.py b/catalog/test_catalog.py index d66e1cb..8493f3f 100755 --- a/catalog/test_catalog.py +++ b/catalog/test_catalog.py @@ -40,6 +40,9 @@ class UpdateCatalogTests(unittest.TestCase): self.assertEqual(authors.getValue('author[0]/email'), 'ww@wright.com') self.assertEqual(authors.getValue('author[1]/name'), 'Orville Wright') + locDe = info['localized']['de'] + self.assertEqual(locDe["long-description"], "Describe the F16-A in German") + def test_scan_dir(self): (pkg, variants) = catalog.scan_aircraft_dir("testData/Aircraft/f16", ["testData/OtherDir"]) @@ -60,6 +63,9 @@ class UpdateCatalogTests(unittest.TestCase): self.assertEqual(f16b['variant-of'], 'f16a') self.assertEqual(f16b['primary-set'], False) + locFr = f16b['localized']['fr'] + self.assertEqual(locFr["long-description"], "Describe the F16-B in French") + authorsArray = f16b['authors'] self.assertNotIn('author', f16b) @@ -135,8 +141,9 @@ class UpdateCatalogTests(unittest.TestCase): self.assertEqual(parsedPkgNode.getValue('rating/cockpit'), 2) self.assertEqual(parsedPkgNode.getValue('rating/model'), 5) + self.assertEqual(parsedPkgNode.getValue('localized/de/long-description'), "Describe the F16-A in German") -# author data verification + # author data verification self.assertFalse(parsedPkgNode.hasChild('author')); parsedAuthors = parsedPkgNode.getChild("authors").getChildren('author') @@ -172,6 +179,9 @@ class UpdateCatalogTests(unittest.TestCase): self.assertEqual(author1.getValue("email"), "shatner@enterprise.com") self.assertEqual(author1.getValue("description"), "Everything") + self.assertEqual(pv.getValue('localized/de/long-description'), "Describe the F16-B in German") + + def test_node_creation2(self): (pkg, variants) = catalog.scan_aircraft_dir("testData/Aircraft/dc3", ["testData/OtherDir"]) diff --git a/python3-flightgear/flightgear/meta/aircraft_catalogs/catalog.py b/python3-flightgear/flightgear/meta/aircraft_catalogs/catalog.py index 36e0dea..f115ffd 100644 --- a/python3-flightgear/flightgear/meta/aircraft_catalogs/catalog.py +++ b/python3-flightgear/flightgear/meta/aircraft_catalogs/catalog.py @@ -11,7 +11,7 @@ import re import sys import zipfile -from flightgear.meta import sgprops +from flightgear.meta import sgprops, strutils from . import catalogTags CATALOG_VERSION = 4 @@ -113,9 +113,13 @@ def scan_set_file(aircraft_dir, set_file, includes): if sim_node.hasChild('minimum-fg-version'): variant['minimum-fg-version'] = sim_node.getValue('minimum-fg-version', None) + if sim_node.hasChild('localized'): + variant['localized'] = extract_localized_strings(sim_node.getChild('localized')) + #print(" %s" % variant) return variant + def extract_previews(previews_node, aircraft_dir): result = [] for node in previews_node.getChildren("preview"): @@ -131,6 +135,7 @@ def extract_previews(previews_node, aircraft_dir): return result + def extract_tags(tags_node, set_path): result = [] for node in tags_node.getChildren("tag"): @@ -142,6 +147,22 @@ def extract_tags(tags_node, set_path): return result + +def extract_localized_strings(localized_node): + result = {} + # iterate langauges below + for lang in localized_node.getChildren(): + strings = {} + + # iterate strings below etc + for s in lang.getChildren(): + strings[s.name] = strutils.simplify(s.value) + + if strings: + result[lang.name] = strings + + return result + # scan all the -set.xml files in an aircraft directory. Returns a # package dict and a list of variants. def scan_aircraft_dir(aircraft_dir, includes): @@ -199,6 +220,7 @@ def make_xml_leaf(name, text): leaf.text = '' return leaf + def append_preview_nodes(node, variant, download_base, package_name): if not 'previews' in variant: return @@ -211,6 +233,7 @@ def append_preview_nodes(node, variant, download_base, package_name): preview_node.append( make_xml_leaf('path', preview['path']) ) node.append(preview_node) + def append_tag_nodes(node, variant): if not 'tags' in variant: return @@ -218,6 +241,7 @@ def append_tag_nodes(node, variant): for tag in variant['tags']: node.append(make_xml_leaf('tag', tag)) + def append_author_nodes(node, info): if 'authors' in info: node.append(info['authors']._createXMLElement()) @@ -225,6 +249,22 @@ def append_author_nodes(node, info): # traditional single author string node.append( make_xml_leaf('author', info['author']) ) + +def append_localized_strings(node, variant): + if not 'localized' in variant: + return + + localized_node = ET.Element('localized') + for lang, v in variant['localized'].items(): + lang_node = ET.Element(lang) + for skey, s in v.items(): + lang_node.append(make_xml_leaf(skey, s)) + + localized_node.append(lang_node) + + node.append(localized_node) + + def make_aircraft_node(aircraftDirName, package, variants, downloadBase, mirrors): #print("package: %s" % package) #print("variants: %s" % variants) @@ -271,6 +311,7 @@ def make_aircraft_node(aircraftDirName, package, variants, downloadBase, mirrors append_preview_nodes(variant_node, variant, downloadBase, aircraftDirName) append_tag_nodes(variant_node, variant) append_author_nodes(variant_node, variant) + append_localized_strings(variant_node, variant) package_node.append( make_xml_leaf('dir', aircraftDirName) ) @@ -290,6 +331,7 @@ def make_aircraft_node(aircraftDirName, package, variants, downloadBase, mirrors append_preview_nodes(package_node, package, downloadBase, aircraftDirName) append_tag_nodes(package_node, package) + append_localized_strings(package_node, package) if 'maintainers' in package: package_node.append(package['maintainers']._createXMLElement())