make_i18n.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Embeds the translation tables in the executable.
  2. # Usage: python3 tools/make_i18n.py (from the repository root)
  3. # Reads every resources/i18n/<id>.txt and writes src/ui/i18n/<id>.h (the table zlib-compressed: the plain
  4. # tables are ~390 KB each, compressed ~120 KB; one header per language so a fix to one table changes one file)
  5. # plus src/ui/I18nData.h (the list). Unpacked with lynx::inflateZlib when a language is chosen.
  6. import pathlib, zlib
  7. root = pathlib.Path(__file__).resolve().parent.parent
  8. # fixed order: the order of the Language menu is set in src/ui/I18n.cpp, this one only names the arrays
  9. ids = sorted(p.stem for p in (root / "resources/i18n").glob("*.txt"))
  10. out = ["#pragma once", "",
  11. "// Generated by tools/make_i18n.py from resources/i18n/*.txt - do not edit by hand.", "",
  12. "#include <cstddef>", "",
  13. "namespace lynx::i18n::data", "{",
  14. "struct Table",
  15. "{",
  16. " const char* id; // \"pt-BR\"",
  17. " const unsigned char* zlib; // the .txt file, zlib-compressed",
  18. " std::size_t zlibSize, textSize; // compressed / original size in bytes",
  19. "};", "} // namespace lynx::i18n::data", ""]
  20. names = []
  21. folder = root / "src/ui/i18n"
  22. folder.mkdir(exist_ok=True)
  23. for stale in folder.glob("*.h"):
  24. if stale.stem not in ids:
  25. stale.unlink()
  26. for lang in ids:
  27. data = (root / f"resources/i18n/{lang}.txt").read_bytes()
  28. packed = zlib.compress(data, 9)
  29. name = "t_" + lang.replace("-", "_")
  30. names.append((lang, name, len(packed), len(data)))
  31. part = ["#pragma once", "",
  32. f"// Generated by tools/make_i18n.py from resources/i18n/{lang}.txt ({len(data)} bytes, zlib) - do not edit by hand.",
  33. "", "namespace lynx::i18n::data", "{",
  34. f"inline constexpr unsigned char {name}[] = {{"]
  35. for i in range(0, len(packed), 32):
  36. part.append(",".join(str(b) for b in packed[i:i + 32]) + ",")
  37. part += ["};", "} // namespace lynx::i18n::data", ""]
  38. (folder / f"{lang}.h").write_text("\n".join(part))
  39. out.append(f'#include "ui/i18n/{lang}.h"')
  40. out += ["", "namespace lynx::i18n::data", "{", "inline constexpr Table tables[] = {"]
  41. for lang, name, n, size in names:
  42. out.append(f' {{ "{lang}", {name}, {n}, {size} }},')
  43. out.append("};")
  44. out.append("} // namespace lynx::i18n::data")
  45. out.append("")
  46. (root / "src/ui/I18nData.h").write_text("\n".join(out))
  47. print(f"{len(names)} table(s) -> src/ui/i18n/*.h + src/ui/I18nData.h")
  48. # The English display table (rollup 0.1.31 step 3): resources/i18n/en/display.txt, a few lines that change what English
  49. # shows without changing the tr() keys every table is keyed by; small, so embedded as plain text in src/ui/I18nEnglish.h
  50. en = (root / "resources/i18n/en/display.txt").read_text(encoding="utf-8")
  51. 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)
  52. for b in en.encode("utf-8"))
  53. (root / "src/ui/I18nEnglish.h").write_text("\n".join([
  54. "#pragma once", "",
  55. "// Generated by tools/make_i18n.py from resources/i18n/en/display.txt - do not edit by hand.", "",
  56. "namespace lynx::i18n::data", "{",
  57. "// what English shows for a few tr() keys (JUCE LocalisedStrings text)",
  58. "inline constexpr const char* englishDisplay =",
  59. " \"" + body + "\";",
  60. "} // namespace lynx::i18n::data", ""]))
  61. print("English display table -> src/ui/I18nEnglish.h")