Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate translation files #880

Merged
merged 3 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@ desktop.ini

# User additions
/zzz_backup
/arm9/source/language.inl
*.trf
23 changes: 20 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ ifeq ($(NTRBOOT),1)
VRAM_SCRIPTS := resources/gm9/scripts
endif

# Definitions for translation files
JSON_FOLDER := resources/languages
TRF_FOLDER := resources/gm9/languages

SOURCE_JSON := $(JSON_FOLDER)/source.json
LANGUAGE_INL := arm9/source/language.inl

JSON_FILES := $(filter-out $(SOURCE_JSON),$(wildcard $(JSON_FOLDER)/*.json))
TRF_FILES := $(subst $(JSON_FOLDER),$(TRF_FOLDER),$(JSON_FILES:.json=.trf))

ifeq ($(OS),Windows_NT)
ifeq ($(TERM),cygwin)
PY3 := py -3 # Windows / CMD/PowerShell
Expand Down Expand Up @@ -51,7 +61,7 @@ clean:
@set -e; for elf in $(ELF); do \
$(MAKE) --no-print-directory -C $$(dirname $$elf) clean; \
done
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR)
@rm -rf $(OUTDIR) $(RELDIR) $(FIRM) $(FIRMD) $(VRAM_TAR) $(LANGUAGE_INL) $(TRF_FILES)

unmarked_readme: .FORCE
@$(PY3) utils/unmark.py -f README.md data/README_internal.md
Expand Down Expand Up @@ -85,13 +95,20 @@ $(VRAM_TAR): $(SPLASH) $(OVERRIDE_FONT) $(VRAM_DATA) $(VRAM_SCRIPTS)
@echo "Creating $@"
@$(PY3) utils/add2tar.py $(VRAM_FLAGS) $(VRAM_TAR) $(shell find $^ -type f)

$(LANGUAGE_INL): $(SOURCE_JSON)
@echo "Creating $@"
@$(PY3) utils/transcp.py $< $@

$(TRF_FOLDER)/%.trf: $(JSON_FOLDER)/%.json
@$(PY3) utils/transriff.py $< $@

%.elf: .FORCE
@echo "Building $@"
@$(MAKE) --no-print-directory -C $(@D)

arm9/arm9.elf: $(VRAM_TAR)
arm9/arm9.elf: $(VRAM_TAR) $(LANGUAGE_INL)

firm: $(ELF)
firm: $(ELF) $(TRF_FILES)
@mkdir -p $(call dirname,"$(FIRM)") $(call dirname,"$(FIRMD)")
@echo "[FLAVOR] $(FLAVOR)"
@echo "[VERSION] $(VERSION)"
Expand Down
809 changes: 0 additions & 809 deletions arm9/source/language.inl

This file was deleted.

Binary file removed resources/gm9/languages/de.trf
Binary file not shown.
Binary file removed resources/gm9/languages/en.trf
Binary file not shown.
Binary file removed resources/gm9/languages/es.trf
Binary file not shown.
Binary file removed resources/gm9/languages/fr.trf
Binary file not shown.
Binary file removed resources/gm9/languages/id.trf
Binary file not shown.
Binary file removed resources/gm9/languages/it.trf
Binary file not shown.
Binary file removed resources/gm9/languages/ja.trf
Binary file not shown.
Binary file removed resources/gm9/languages/nl.trf
Binary file not shown.
Binary file removed resources/gm9/languages/pl.trf
Binary file not shown.
Binary file removed resources/gm9/languages/ru.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
26 changes: 26 additions & 0 deletions utils/transcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

from argparse import ArgumentParser, FileType
import json

# 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 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" % version)
for key in source:
# Escape \r, \n, and quotes
val = source[key].replace("\r", "\\r").replace("\n", "\\n").replace('"', '\\"')
args.inl.write('STRING(%s, "%s")\n' % (key, val))
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)