ci/translations: Add a linter to check for invalid characters, fix current issues (#2774)

This commit is contained in:
Tyler Wilding
2023-06-25 14:13:32 -05:00
committed by GitHub
parent cb895d4ba0
commit 4018d15fde
11 changed files with 138 additions and 25 deletions
+109
View File
@@ -0,0 +1,109 @@
import glob
import json
import re
# TODO - add a way to make this auto replace bad characters with `?`
# fmt: off
JAK1_ALLOWED_CHARACTERS = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"'", "!", "(", ")", "+", "-", ",", ".", "/", ":", "=", "<", ">", "*", "%", "?", "\"",
"`", "ˇ", "¨", "º", "¡", "¿", "Æ", "Ç", "ß", "", "", " ", "Å", "Ø",
"Ñ", "Ã", "Õ", "Á", "É", "Í", "Ó", "Ú", "Ő", "Ű", "Â", "Ê", "Î", "Ô", "Û", "À", "È", "Ì", "Ò", "Ù", "Ä", "Ë", "Ï", "Ö", "ö", "Ü",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Œ", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "",
"~"
]
JAK1_ALLOWED_CODES = [
"<TIL>",
"<PAD_X>", "<PAD_TRIANGLE>", "<PAD_CIRCLE>", "<PAD_SQUARE>"
]
# fmt: on
invalid_characters_found = False
def is_allowed_code(pos, text):
# Find any occurences of allowed codes in the string
# if the position overlaps with these occurrences, it's allowed
for code in JAK1_ALLOWED_CODES:
for match in re.finditer(code, text):
if pos >= match.start() and pos <= match.end():
return match.end()
return -1
def char_allowed(char):
return char in JAK1_ALLOWED_CHARACTERS
def lint_jak1_characters(text):
invalid_characters_found = False
pos = 0
while pos < len(text):
character = text[pos]
if not char_allowed(character):
# Check to see if it's an allowed code
code_end_pos = is_allowed_code(pos, text)
if code_end_pos == -1:
print(
"Character '{}' not allowed - Found in {}".format(character, text)
)
invalid_characters_found = True
pos = pos + 1
else:
# advance to the end of the code and continue checking
pos = code_end_pos
else:
pos = pos + 1
return invalid_characters_found
# Iterate through the translations making sure there are no characters that are not allowed
text_files = glob.glob("./game/assets/jak1/text/*.json")
for text_file in text_files:
print("Checking {}...".format(text_file))
with open(text_file, encoding="utf-8") as f:
file_data = json.load(f)
for id, text in file_data.items():
invalid_chars_exist = lint_jak1_characters(text)
if invalid_chars_exist:
invalid_characters_found = True
subtitle_files = glob.glob("./game/assets/jak1/subtitle/*lines*.json")
for subtitle_file in subtitle_files:
print("Checking {}...".format(subtitle_file))
with open(subtitle_file, encoding="utf-8") as f:
file_data = json.load(f)
# Check Speakers
for id, text in file_data["speakers"].items():
invalid_chars_exist = lint_jak1_characters(text)
if invalid_chars_exist:
invalid_characters_found = True
# Check Lines
for id, lines in file_data["cutscenes"].items():
for line in lines:
invalid_chars_exist = lint_jak1_characters(line)
if invalid_chars_exist:
invalid_characters_found = True
for id, lines in file_data["hints"].items():
for line in lines:
invalid_chars_exist = lint_jak1_characters(line)
if invalid_chars_exist:
invalid_characters_found = True
if invalid_characters_found:
print("Invalid characters were found, see above")
exit(1)
else:
print("No invalid characters found!")