Skip to content

Commit

Permalink
Move version number to JSON file
Browse files Browse the repository at this point in the history
Better than being a magic number somewhere in the code, source code gets it from source.json
  • Loading branch information
Epicpkmn11 committed Nov 29, 2024
1 parent a0ecbfe commit 47e5ea2
Show file tree
Hide file tree
Showing 18 changed files with 45 additions and 18 deletions.
Binary file modified resources/gm9/languages/es.trf
Binary file not shown.
Binary file added resources/gm9/languages/ja-KANA.trf
Binary file not shown.
Binary file added resources/gm9/languages/ry.trf
Binary file not shown.
1 change: 1 addition & 0 deletions resources/languages/de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Deutsch",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX.%3$02lX.%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": ".",
Expand Down
3 changes: 2 additions & 1 deletion resources/languages/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"GM9_LANGUAGE": "English"
"GM9_LANGUAGE": "English",
"GM9_TRANS_VER": 1
}
1 change: 1 addition & 0 deletions resources/languages/es.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Español",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX/%3$02lX/%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": ".",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/fr.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Français",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX/%3$02lX/%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": " ",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/id.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Bahasa Indonesia",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "(Thn-Bln-Hari) %1$s%2$02lX-%3$02lX-%4$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ".",
"THOUSAND_SEPARATOR": ",",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/it.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Italiano",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX/%3$02lX/%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": ".",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/ja-KANA.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "にほんご",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%1$s%2$02lXねん%3$02lXがつ%4$02lXにち %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ".",
"THOUSAND_SEPARATOR": ",",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/ja.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "日本語",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%1$s%2$02lX年%3$02lX月%4$02lX日 %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ".",
"THOUSAND_SEPARATOR": ",",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/nl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Nederlands",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX-%3$02lX-%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": ".",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/pl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Polski",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX.%3$02lX.%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": " ",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/ru.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "Русский",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%4$02lX.%3$02lX.%1$s%2$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ",",
"THOUSAND_SEPARATOR": " ",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/ry.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "琉球諸語(うちなーぐちー)",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%1$s%2$02lXとぅし%3$02lXちち%4$02lX日 %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ".",
"THOUSAND_SEPARATOR": ",",
Expand Down
1 change: 1 addition & 0 deletions resources/languages/source.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"GM9_LANGUAGE": "[LANGUAGE NAME]",
"GM9_TRANS_VER": 1,
"DATE_TIME_FORMAT": "%1$s%2$02lX-%3$02lX-%4$02lX %5$02lX:%6$02lX",
"DECIMAL_SEPARATOR": ".",
"THOUSAND_SEPARATOR": ",",
Expand Down
13 changes: 6 additions & 7 deletions utils/transcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
from argparse import ArgumentParser, FileType
import json

# This is to be incremented every time the order changes
# New strings added to the end will not cause issues
TRANSLATION_VER = 1

# This is the language name key in the JSON, which is to be deleted
# Special keys
LANGUAGE_NAME = "GM9_LANGUAGE"
VERSION = "GM9_TRANS_VER"

parser = ArgumentParser(description="Creates the language.inl file from source.json")
parser.add_argument("source", type=FileType("r"), help="source.json")
parser.add_argument("inl", type=FileType("w"), help="language.inl")
args = parser.parse_args()

# Load the JSON and remove the language name
# Load the JSON and handle the meta values
source = json.load(args.source)
version = source[VERSION]
del source[VERSION]
del source[LANGUAGE_NAME]

# Create the header file
args.inl.write("#define TRANSLATION_VER %d\n\n" % TRANSLATION_VER)
args.inl.write("#define TRANSLATION_VER %d\n\n" % version)
for key in source:
# Escape \r, \n, and quotes
val = source[key].replace("\r", "\\r").replace("\n", "\\n").replace('"', '\\"')
Expand Down
35 changes: 25 additions & 10 deletions utils/transriff.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import sys


# Special keys
LANGUAGE_NAME = "GM9_LANGUAGE"
VERSION = "GM9_TRANS_VER"


def ceil_to_multiple(num: int, base: int) -> int:
Expand Down Expand Up @@ -43,7 +45,25 @@ def get_language(data: dict) -> bytes:
try:
return data[LANGUAGE_NAME].encode("utf-8")
except KeyError as exception:
raise ValueError("invalid language data") from exception
raise ValueError("missing language name") from exception

def get_version(data: dict) -> int:
"""
Get translation version from JSON data.
Args:
data: JSON translation data.
Returns:
The translation's version.
Raises:
ValueError: If no version exists.
"""
try:
return data[VERSION]
except KeyError as exception:
raise ValueError("missing verison number") from exception


def load_translations(data: dict) -> dict[str, bytearray]:
Expand All @@ -59,7 +79,7 @@ def load_translations(data: dict) -> dict[str, bytearray]:
return {
key: bytearray(value, "utf-8") + b"\0"
for key, value in data.items()
if key != LANGUAGE_NAME
if key not in (LANGUAGE_NAME, VERSION)
}


Expand Down Expand Up @@ -119,19 +139,19 @@ def strings_to_trf(mapping: dict[str, bytearray], version: int, language: str) -
return trfdata


def main(source: pathlib.Path, dest: pathlib.Path, version: int) -> None:
def main(source: pathlib.Path, dest: pathlib.Path) -> None:
"""
Entrypoint of transriff.
Args:
source: JSON to convert from.
dest: TRF file to write.
version: Translation version.
"""
data = json.loads(source.read_text())

try:
language = get_language(data)
version = get_version(data)
except ValueError as exception:
sys.exit(f"Fatal: {exception}.")
mapping = load_translations(data)
Expand All @@ -157,12 +177,7 @@ def main(source: pathlib.Path, dest: pathlib.Path, version: int) -> None:
type=pathlib.Path,
help="TRF file to write"
)
parser.add_argument(
"version",
type=int,
help="translation version, from language.yml"
)

args = parser.parse_args()

main(args.source, args.dest, args.version)
main(args.source, args.dest)

0 comments on commit 47e5ea2

Please sign in to comment.