2021-09-03 16:35:03 +02:00
|
|
|
import re
|
|
|
|
import json
|
|
|
|
import glob
|
|
|
|
|
|
|
|
# List all locale files (except en.json being the ref)
|
|
|
|
locale_folder = "locales/"
|
|
|
|
locale_files = glob.glob(locale_folder + "*.json")
|
|
|
|
locale_files = [filename.split("/")[-1] for filename in locale_files]
|
|
|
|
locale_files.remove("en.json")
|
|
|
|
|
|
|
|
reference = json.loads(open(locale_folder + "en.json").read())
|
|
|
|
|
|
|
|
|
|
|
|
def fix_locale(locale_file):
|
|
|
|
this_locale = json.loads(open(locale_folder + locale_file).read())
|
|
|
|
fixed_stuff = False
|
|
|
|
|
|
|
|
# We iterate over all keys/string in en.json
|
|
|
|
for key, string in reference.items():
|
|
|
|
# Ignore check if there's no translation yet for this key
|
|
|
|
if key not in this_locale:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Then we check that every "{stuff}" (for python's .format())
|
|
|
|
# should also be in the translated string, otherwise the .format
|
|
|
|
# will trigger an exception!
|
|
|
|
subkeys_in_ref = [k[0] for k in re.findall(r"{(\w+)(:\w)?}", string)]
|
2021-09-03 16:43:55 +02:00
|
|
|
subkeys_in_this_locale = [
|
|
|
|
k[0] for k in re.findall(r"{(\w+)(:\w)?}", this_locale[key])
|
|
|
|
]
|
2021-09-03 16:35:03 +02:00
|
|
|
|
2021-09-03 16:43:55 +02:00
|
|
|
if set(subkeys_in_ref) != set(subkeys_in_this_locale) and (
|
|
|
|
len(subkeys_in_ref) == len(subkeys_in_this_locale)
|
|
|
|
):
|
2021-09-03 16:35:03 +02:00
|
|
|
for i, subkey in enumerate(subkeys_in_ref):
|
2021-09-03 16:43:55 +02:00
|
|
|
this_locale[key] = this_locale[key].replace(
|
|
|
|
"{%s}" % subkeys_in_this_locale[i], "{%s}" % subkey
|
|
|
|
)
|
2021-09-03 16:35:03 +02:00
|
|
|
fixed_stuff = True
|
|
|
|
|
|
|
|
if fixed_stuff:
|
|
|
|
json.dump(
|
|
|
|
this_locale,
|
|
|
|
open(locale_folder + locale_file, "w"),
|
|
|
|
indent=4,
|
|
|
|
ensure_ascii=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
for locale_file in locale_files:
|
|
|
|
fix_locale(locale_file)
|