1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-07-01 01:23:30 +00:00
CC-Tweaked/tools/language.py
Jonathan Coates 08df68dcc0
Generate en_us.json via datagen
I was originally pretty sceptical about this, but it actually ends up
being useful for the same reason any other form of datagen is: we can
ensure that names are well formed, and that every string is actually
translated.

There's some future work here to go through all the custom translation
keys and move them into constants (maybe also do something with the
/computercraft command?), but that's a separate chunk of work.

The main motivation for this is to add translation keys to our config:
the Fabric version of Forge Config API provides a config UI, so it's
useful to provide user-friendly strings. Our generator also
automatically copies comments over, turning them into tooltips.

This also updates all of the other language files to match en_us.json
again: it's a very noisy diff as the file is now sorted alphabetically.
Hopefully this won't affect weblate though

[^1]: Amusing really that the Fabric port actually is more useful than
the original.
2022-11-20 13:00:43 +00:00

45 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Rewrites language files in order to be consistent with en_us.
This will take every given language file and rewrite it to be in the same
order as the en_us file. Any keys which appear in the given file, but not
in en_us are removed.
Note, this is not intended to be a fool-proof tool, rather a quick way to
ensure language files are mostly correct.
"""
import json
import pathlib
from collections import OrderedDict
root = pathlib.Path("projects/common/src/main/resources/assets/computercraft/lang")
with open("projects/fabric/src/generated/resources/assets/computercraft/lang/en_us.json", encoding="utf-8") as file:
en_us = json.load(file, object_hook=OrderedDict)
for path in root.glob("*.json"):
if path.name == "en_us.json":
continue
with path.open(encoding="utf-8") as file:
lang = json.load(file)
out = OrderedDict()
missing = 0
for k in en_us.keys():
if k not in lang:
missing += 1
elif lang[k] == "":
print("{} has empty translation for {}".format(path.name, k))
else:
out[k] = lang[k]
with path.open("w", encoding="utf-8", newline="\n") as file:
json.dump(out, file, indent=4, ensure_ascii=False)
file.write("\n")
if missing > 0:
print("{} has {} missing translations.".format(path.name, missing))