Skip to content

Commit

Permalink
Added .json support. No longer works with .lang files
Browse files Browse the repository at this point in the history
  • Loading branch information
rogama25 committed Apr 30, 2019
1 parent 2a224e2 commit 791ad0b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
44 changes: 14 additions & 30 deletions Translations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

class Translations:
def __init__(self):
self.first_file_lines = None
Expand All @@ -10,48 +12,30 @@ def __init__(self):
self.second_file_uri = None

def load_first(self):
self.first_file_lines = []
self.is_translation_line = []
self.first_file_keys = []
self.first_file_translations = {}
with open(self.first_file_uri, mode='r+', encoding="utf-8") as file:
for l in file:
if ("=" not in l) or l.startswith("#"):
self.first_file_lines.append(l)
self.is_translation_line.append(False)
else:
key, value = l.split("=", 1)
self.first_file_lines.append(key)
self.is_translation_line.append(True)
self.first_file_keys.append(key)
if value.endswith("\n"):
value = value[:-1]
self.first_file_translations[key] = value
self.first_file_translations = json.load(file)
for k in self.first_file_translations:
if k.startswith("_"):
self.first_file_translations.pop(k, None)
self.first_file_translations.pop("_comment", None)

def load_second(self):
self.second_file_translations = {}
with open(self.second_file_uri, mode='r+', encoding="utf-8") as file:
for l in file:
if ("=" in l) and not l.startswith("#"):
key, value = l.split("=", 1)
if value.endswith("\n"):
value = value[:-1]
self.second_file_translations[key] = value
self.recalculate_common()
self.second_file_translations = json.load(file)
for k in self.second_file_translations:
if k.startswith("_"):
self.second_file_translations.pop(k, None)
self.recalculate_common()

def recalculate_common(self):
self.common_keys = []
for key in self.first_file_keys:
for key in self.first_file_translations:
if key in self.second_file_translations:
self.common_keys.append(key)

def save_second(self):
line_number = 0
with open(self.second_file_uri, "w+", encoding="utf-8") as file:
for line in self.first_file_lines:
if self.is_translation_line[line_number]:
if line in self.second_file_translations:
file.write(line + "=" + self.second_file_translations[line] + "\n")
else:
file.write(line)
line_number += 1
json.dump(self.second_file_translations, file, ensure_ascii=False, indent="\t")
2 changes: 1 addition & 1 deletion console_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def open_file(tr: Translations, num: int, uri: str = None):
if uri is None:
print("Please select the file #" + str(num) + " in the next window. Press Enter to continue.")
press_enter()
uri = easygui.fileopenbox(title="Open file #" + str(num), default='*.lang', filetypes=["*.lang"])
uri = easygui.fileopenbox(title="Open file #" + str(num), default='*.json', filetypes=["*.json"])
if uri is None:
return -1
if num == 1:
Expand Down

0 comments on commit 791ad0b

Please sign in to comment.