Created
December 4, 2016 18:08
-
-
Save eloo/40f1d834f9db26bb3f2bf92eea556775 to your computer and use it in GitHub Desktop.
OpenPHT language file merge
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 characters
import re | |
ENGLISH_STRING_FILE = "OpenPHT/language/English_plex/strings.po" | |
GERMAN_STRING_FILE = "OpenPHT/language/German_plex/strings.po" | |
MESSAGE_CONTEXT_KEY = "msgctxt" | |
MESSAGE_ID_KEY = "msgid" | |
MESSAGE_STRING_KEY = "msgstr" | |
def init_language(file_path): | |
content = get_file_content(file_path) | |
msg_obj = [] | |
msg_ctx = 0 | |
lan_dict = {} | |
for index, line in enumerate(content): | |
if line.startswith(MESSAGE_CONTEXT_KEY): | |
lan_dict[msg_ctx] = msg_obj | |
msg_obj = [] | |
msg_ctx = re.search('\"#(.+?)\"', line).group(1) | |
if msg_obj is not None: | |
msg_obj.append(line) | |
lan_dict[msg_ctx] = msg_obj | |
return lan_dict | |
def get_file_content(filename): | |
with open(filename) as f: | |
content = f.readlines() | |
return content | |
def write_dict(d, filename): | |
keylist = d.keys() | |
keylist.sort(key=int) | |
print keylist | |
with open(filename, 'w') as f: | |
for key in keylist: | |
for line in d[key]: | |
f.write(line) | |
def merge_languages(main_dict, additional_dict): | |
for key, value in additional_dict.items(): | |
if main_dict.has_key(key): | |
continue | |
else: | |
main_dict[key] = value | |
return main_dict | |
def main(): | |
english = init_language(ENGLISH_STRING_FILE) | |
german = init_language(GERMAN_STRING_FILE) | |
merged = merge_languages(german, english) | |
write_dict(merged, "german.po") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copy this script parallel to your cloned OpenPHT repository and customize the script to your needs (language) and run it like normal python scripts.