Created
December 12, 2017 07:41
-
-
Save myrdd/d3d3778a869c81938a89b889dee7e77a to your computer and use it in GitHub Desktop.
Revisions
-
myrdd created this gist
Dec 12, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,126 @@ #!/usr/bin/python3 # source: https://github.com/RequestPolicyContinued/requestpolicy/issues/876 import os import re default_locale = 'en-US' locales = [ 'de', 'en-US', 'eo', 'es-ES', 'es-MX', 'eu', 'fr', 'it', 'ja', 'ko-KR', 'lv-LV', 'nl', 'pl', 'pt-BR', 'ru-RU', 'sk-SK', 'sv-SE', 'tr', 'uk-UA', 'zh-CN', 'zh-TW', ] TAB = ' ' def src_path(locale): return "src/locale/{}/requestpolicy.properties".format(locale) def dest_path(locale): locale = locale.replace('-', '_') return "src/conditional/webextension/_locales/{}/messages.json".format(locale) def addon_description(locale, list_): with open('src/conditional/legacy/install.rdf', 'r') as s: match = re.search(r''' \s* <em:localized> \s* <Description> \s* <em:locale>{}</em:locale> \s* <em:name>([^<]*)</em:name> \s* <em:description>([^<]*)</em:description> \s* </Description> \s* </em:localized> \s* '''.format(locale), s.read().replace('\n', ' '), re.VERBOSE) if match is not None: list_.insert(0, 'extensionName=' + match.group(1)) list_.insert(1, 'extensionDescription=' + match.group(2)) def map_str(str_): return ( str_ .replace('%S', '$1') .replace('%1$S', '$1') .replace('%2$S', '$2') .replace(r'\"', r'"') .replace('"', r'\"') ) def get_list(locale): l = [] with open(src_path(locale), "r") as s: l = s.read().split("\n") addon_description(locale, l) l = [map_str(s).split('=') for s in l if s != ''] return l def get_dict(locale): l = get_list(locale) d = {} for [key, value] in l: d[key] = value return d def reduce_dict(d, d_default): for key in list(d.keys()): assert key in d_default if d[key] == d_default[key]: del d[key] def key_order(): l = get_list(default_locale) for [key, _] in l: yield key def get_entries(d, d_default): for key in key_order(): if key not in d: continue tvalue = d[key] dvalue = d_default[key] entry = "" entry += (TAB*1) + "\"" + key + "\": {\n" entry += (TAB*2) + "\"message\": \"" + tvalue + "\",\n" entry += (TAB*2) + "\"description\": \"" + dvalue + "\"\n" entry += TAB + "}" yield entry def main(): print("Converting from .properties to messages.json...") d_default = get_dict(default_locale) for locale in locales: print(locale) d = get_dict(locale) if locale != default_locale: reduce_dict(d, d_default) destfile_path = dest_path(locale) destdir_path = os.path.dirname(destfile_path) if not os.path.exists(destdir_path): os.makedirs(destdir_path) # Write messages.json file with open(destfile_path, "w") as f: f.write("{\n") entries = list(get_entries(d, d_default)) f.write(",\n\n".join(entries)) f.write("\n}\n") print("Done.") main()