| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- # Embeds the translation tables in the executable.
- # Usage: python3 tools/make_i18n.py (from the repository root)
- # Reads every resources/i18n/<id>.txt and writes src/ui/i18n/<id>.h (the table zlib-compressed: the plain
- # tables are ~390 KB each, compressed ~120 KB; one header per language so a fix to one table changes one file)
- # plus src/ui/I18nData.h (the list). Unpacked with lynx::inflateZlib when a language is chosen.
- import pathlib, zlib
- root = pathlib.Path(__file__).resolve().parent.parent
- # fixed order: the order of the Language menu is set in src/ui/I18n.cpp, this one only names the arrays
- ids = sorted(p.stem for p in (root / "resources/i18n").glob("*.txt"))
- out = ["#pragma once", "",
- "// Generated by tools/make_i18n.py from resources/i18n/*.txt - do not edit by hand.", "",
- "#include <cstddef>", "",
- "namespace lynx::i18n::data", "{",
- "struct Table",
- "{",
- " const char* id; // \"pt-BR\"",
- " const unsigned char* zlib; // the .txt file, zlib-compressed",
- " std::size_t zlibSize, textSize; // compressed / original size in bytes",
- "};", "} // namespace lynx::i18n::data", ""]
- names = []
- folder = root / "src/ui/i18n"
- folder.mkdir(exist_ok=True)
- for stale in folder.glob("*.h"):
- if stale.stem not in ids:
- stale.unlink()
- for lang in ids:
- data = (root / f"resources/i18n/{lang}.txt").read_bytes()
- packed = zlib.compress(data, 9)
- name = "t_" + lang.replace("-", "_")
- names.append((lang, name, len(packed), len(data)))
- part = ["#pragma once", "",
- f"// Generated by tools/make_i18n.py from resources/i18n/{lang}.txt ({len(data)} bytes, zlib) - do not edit by hand.",
- "", "namespace lynx::i18n::data", "{",
- f"inline constexpr unsigned char {name}[] = {{"]
- for i in range(0, len(packed), 32):
- part.append(",".join(str(b) for b in packed[i:i + 32]) + ",")
- part += ["};", "} // namespace lynx::i18n::data", ""]
- (folder / f"{lang}.h").write_text("\n".join(part))
- out.append(f'#include "ui/i18n/{lang}.h"')
- out += ["", "namespace lynx::i18n::data", "{", "inline constexpr Table tables[] = {"]
- for lang, name, n, size in names:
- out.append(f' {{ "{lang}", {name}, {n}, {size} }},')
- out.append("};")
- out.append("} // namespace lynx::i18n::data")
- out.append("")
- (root / "src/ui/I18nData.h").write_text("\n".join(out))
- print(f"{len(names)} table(s) -> src/ui/i18n/*.h + src/ui/I18nData.h")
- # The English display table (rollup 0.1.31 step 3): resources/i18n/en/display.txt, a few lines that change what English
- # shows without changing the tr() keys every table is keyed by; small, so embedded as plain text in src/ui/I18nEnglish.h
- en = (root / "resources/i18n/en/display.txt").read_text(encoding="utf-8")
- body = "".join("\\%03o" % b if b >= 0x80 or b in (0x22, 0x5c) else chr(b) if b >= 0x20 else ("\\n\"\n \"" if b == 0x0a else "\\%03o" % b)
- for b in en.encode("utf-8"))
- (root / "src/ui/I18nEnglish.h").write_text("\n".join([
- "#pragma once", "",
- "// Generated by tools/make_i18n.py from resources/i18n/en/display.txt - do not edit by hand.", "",
- "namespace lynx::i18n::data", "{",
- "// what English shows for a few tr() keys (JUCE LocalisedStrings text)",
- "inline constexpr const char* englishDisplay =",
- " \"" + body + "\";",
- "} // namespace lynx::i18n::data", ""]))
- print("English display table -> src/ui/I18nEnglish.h")
|