diff options
Diffstat (limited to 'util/local_database/xpathlite.py')
-rw-r--r-- | util/local_database/xpathlite.py | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/util/local_database/xpathlite.py b/util/local_database/xpathlite.py index 5bea6ce63a..02cdc65e04 100644 --- a/util/local_database/xpathlite.py +++ b/util/local_database/xpathlite.py @@ -178,7 +178,11 @@ def _findEntryInFile(file, path, draft=None, attribute=None): if elt.attributes.has_key(attribute): return (elt.attributes[attribute].nodeValue, None) return (None, None) - return (elt.firstChild.nodeValue, None) + try: + return (elt.firstChild.nodeValue, None) + except: + pass + return (None, None) def findAlias(file): doc = False @@ -195,6 +199,36 @@ def findAlias(file): return False return alias_elt.attributes['source'].nodeValue +parent_locales = {} +def _fixedLookupChain(dirname, name): + # see http://www.unicode.org/reports/tr35/#Parent_Locales + if not parent_locales: + for ns in findTagsInFile(dirname + "/../supplemental/supplementalData.xml", "parentLocales"): + tmp = {} + parent_locale = "" + for data in ns[1:][0]: # ns looks like this: [u'parentLocale', [(u'parent', u'root'), (u'locales', u'az_Cyrl bs_Cyrl en_Dsrt ..')]] + tmp[data[0]] = data[1] + if data[0] == u"parent": + parent_locale = data[1] + parent_locales[parent_locale] = tmp[u"locales"].split(" ") + + items = name.split("_") + # split locale name into items and iterate through them from back to front + # example: az_Latn_AZ => [az_Latn_AZ, az_Latn, az] + items = list(reversed(map(lambda x: "_".join(items[:x+1]), range(len(items))))) + + for i in range(len(items)): + item = items[i] + for parent_locale in parent_locales.keys(): + for locale in parent_locales[parent_locale]: + if item == locale: + if parent_locale == u"root": + items = items[:i+1] + else: + items = items[:i+1] + parent_locale.split() + items[i+1:] + return items + return items + def _findEntry(base, path, draft=None, attribute=None): file = base if base.endswith(".xml"): @@ -203,10 +237,8 @@ def _findEntry(base, path, draft=None, attribute=None): else: file = base + ".xml" (dirname, filename) = os.path.split(base) - items = filename.split("_") - # split locale name into items and iterate through them from back to front - # example: az_Latn_AZ => [az_Latn_AZ, az_Latn, az] - items = reversed(map(lambda x: "_".join(items[:x+1]), range(len(items)))) + + items = _fixedLookupChain(dirname, filename) for item in items: file = dirname + "/" + item + ".xml" if os.path.isfile(file): |