Editor: Generate translation data in separate cpp files

(cherry picked from commit c93f55533d)
This commit is contained in:
Haoyu Qiu 2025-09-17 23:10:51 +08:00 committed by Rémi Verschelde
parent 42ef49fa99
commit 0e179c0e5c
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 33 additions and 37 deletions

View file

@ -56,36 +56,23 @@ if env.editor_build:
# ratio (20% for the editor UI, 10% for the class reference). # ratio (20% for the editor UI, 10% for the class reference).
# Generated with `make include-list` for each resource. # Generated with `make include-list` for each resource.
# Editor translations translation_targets = {
env.CommandNoCache( "#editor/translations/editor_translations.gen.cpp": Glob("#editor/translations/editor/*"),
"#editor/translations/editor_translations.gen.h", "#editor/translations/property_translations.gen.cpp": Glob("#editor/translations/properties/*"),
Glob("#editor/translations/editor/*"), "#editor/translations/doc_translations.gen.cpp": Glob("#doc/translations/*"),
env.Run(editor_builders.make_translations_header), "#editor/translations/extractable_translations.gen.cpp": Glob("#editor/translations/extractable/*"),
) }
for target_cpp, sources in translation_targets.items():
# Property translations target_h = os.path.splitext(target_cpp)[0] + ".h"
env.CommandNoCache( env.CommandNoCache(
"#editor/translations/property_translations.gen.h", [target_h, target_cpp],
Glob("#editor/translations/properties/*"), sources,
env.Run(editor_builders.make_translations_header), env.Run(editor_builders.make_translations),
) )
# Documentation translations
env.CommandNoCache(
"#editor/translations/doc_translations.gen.h",
Glob("#doc/translations/*"),
env.Run(editor_builders.make_translations_header),
)
# Extractable translations
env.CommandNoCache(
"#editor/translations/extractable_translations.gen.h",
Glob("#editor/translations/extractable/*"),
env.Run(editor_builders.make_translations_header),
)
env.add_source_files(env.editor_sources, "*.cpp") env.add_source_files(env.editor_sources, "*.cpp")
env.add_source_files(env.editor_sources, gen_exporters) env.add_source_files(env.editor_sources, gen_exporters)
env.add_source_files(env.editor_sources, translation_targets.keys())
SConscript("animation/SCsub") SConscript("animation/SCsub")
SConscript("asset_library/SCsub") SConscript("asset_library/SCsub")

View file

@ -68,8 +68,10 @@ inline constexpr const unsigned char _doc_data_compressed[] = {{
""") """)
def make_translations_header(target, source, env): def make_translations(target, source, env):
category = os.path.basename(str(target[0])).split("_")[0] target_h, target_cpp = str(target[0]), str(target[1])
category = os.path.basename(target_h).split("_")[0]
sorted_paths = sorted([src.abspath for src in source], key=lambda path: os.path.splitext(os.path.basename(path))[0]) sorted_paths = sorted([src.abspath for src in source], key=lambda path: os.path.splitext(os.path.basename(path))[0])
xl_names = [] xl_names = []
@ -77,7 +79,7 @@ def make_translations_header(target, source, env):
if not msgfmt: if not msgfmt:
methods.print_warning("msgfmt not found, using .po files instead of .mo") methods.print_warning("msgfmt not found, using .po files instead of .mo")
with methods.generated_wrapper(str(target[0])) as file: with methods.generated_wrapper(target_cpp) as file:
for path in sorted_paths: for path in sorted_paths:
name = os.path.splitext(os.path.basename(path))[0] name = os.path.splitext(os.path.basename(path))[0]
# msgfmt erases non-translated messages, so avoid using it if exporting the POT. # msgfmt erases non-translated messages, so avoid using it if exporting the POT.
@ -120,14 +122,9 @@ inline constexpr const unsigned char _{category}_translation_{name}_compressed[]
xl_names.append([name, len(buffer), decomp_size]) xl_names.append([name, len(buffer), decomp_size])
file.write(f"""\ file.write(f"""\
struct {category.capitalize()}TranslationList {{ #include "{target_h}"
const char* lang;
int comp_size;
int uncomp_size;
const unsigned char* data;
}};
inline constexpr {category.capitalize()}TranslationList _{category}_translations[] = {{ const {category.capitalize()}TranslationList _{category}_translations[] = {{
""") """)
for x in xl_names: for x in xl_names:
@ -136,4 +133,16 @@ inline constexpr {category.capitalize()}TranslationList _{category}_translations
file.write("""\ file.write("""\
{ nullptr, 0, 0, nullptr }, { nullptr, 0, 0, nullptr },
}; };
""")
with methods.generated_wrapper(target_h) as file:
file.write(f"""\
struct {category.capitalize()}TranslationList {{
const char* lang;
int comp_size;
int uncomp_size;
const unsigned char* data;
}};
extern const {category.capitalize()}TranslationList _{category}_translations[];
""") """)