Skip to content

Instantly share code, notes, and snippets.

@Discreater
Last active September 23, 2022 07:54
Show Gist options
  • Save Discreater/7f2768ad8ba455e8842d35afce2ff935 to your computer and use it in GitHub Desktop.
Save Discreater/7f2768ad8ba455e8842d35afce2ff935 to your computer and use it in GitHub Desktop.
Minecraft lang file to ParaTranz JSON
import json
# Lang to JSON
def lang2json(src: str, dst: str, translated: bool):
with open(src, "r") as src_file, open(dst, "w") as dst_file:
lines = src_file.read().splitlines()
items = []
last_i = 0
i = 0
while i < len(lines):
if lines[i] != "" and not lines[i].startswith("#"):
split_pos = lines[i].find('=')
key = lines[i][:split_pos]
value = lines[i][split_pos+1:]
if not translated:
context = '\n'.join(lines[last_i:i])
item = {
"key": key,
"original": value,
"context": context
}
else:
item = {
"key": key,
"translation": value
}
items.append(item)
last_i = i + 1
i += 1
json.dump(items, dst_file, indent=4)
# JSON to lang
def json2lang(src: str, dst: str):
with open(src, "r") as src_file, open(dst, "w") as dst_file:
items = json.load(src_file)
# sort items by id
items = sorted(items, key=lambda item: item["id"])
for item in items:
if item['context'] != "":
dst_file.write(item['context'] + '\n')
dst_file.write(f"{item['key']}={item['translation']}\n")
if __name__ == "__main__":
lang2json("resources/minecraft/lang/en_US.lang", "en_US.json", False)
lang2json("resources/minecraft/lang/zh_CN.lang", "zh_CN.json", True)
# json2lang("en_US.json.json", "quest_translated.lang")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment