godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.17k stars 21.2k forks source link

Custom Translation `ResourceFormatLoader` not parsed at startup #91336

Open RedMser opened 6 months ago

RedMser commented 6 months ago

Tested versions

System information

Godot v4.3.dev (86d0f9b14) - Windows 10.0.19044 - GLES3 (Compatibility) - NVIDIA GeForce RTX 2080 (NVIDIA; 31.0.15.5186) - Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz (12 Threads)

Issue description

When creating a custom ResourceFormatLoader for handling Translation resources, loading files via GDScript works fine.

However, when adding a Translation file via Project Settings > Localization > Translations menu, an error on startup shows and the Translation resource is not loaded:

E 0:00:00:0699   ResourceLoader::_load: No loader found for resource: res://my.test (expected type: )
  <C++ Error>    Method/function failed. Returning: Ref<Resource>()
  <C++ Source>   core\io\resource_loader.cpp:289 @ ResourceLoader::_load()

Steps to reproduce

See the issue description. You can simply open the MRP to see the error and the label not being translated.

If you want to see the loader working, uncomment the code in control.gd. The error persists (until you remove the translation from project settings), but text changes from "broken" to "it works!"

Minimal reproduction project (MRP)

GDScript loader: custom-translation-loader.zip

GDExtension loader: custom-translation-loader-gdextension.zip

RedMser commented 6 months ago

When breakpointing into ResourceLoader::_load, you can see that the loader is missing.

TranslationServer::load_translations is called too early in the startup procedure. This fixes it for GDExtension, but I'm not sure if this possibly breaks anything:

diff --git a/main/main.cpp b/main/main.cpp
index eee634086e..1045c7b45f 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -2964,8 +2964,6 @@ Error Main::setup2() {
                if (!locale.is_empty()) {
                        translation_server->set_locale(locale);
                }
-               translation_server->load_translations();
-               ResourceLoader::load_translation_remaps(); //load remaps for resources

                ResourceLoader::load_path_remaps();

@@ -3064,6 +3062,9 @@ Error Main::setup2() {

        OS::get_singleton()->benchmark_end_measure("Startup", "Scene");

+       translation_server->load_translations();
+       ResourceLoader::load_translation_remaps(); //load remaps for resources
+
 #ifdef TOOLS_ENABLED
        ClassDB::set_current_api(ClassDB::API_EDITOR);
        register_editor_types();

GDScript relies on ResourceLoader::add_custom_loaders() which is called in Main::start()

RedMser commented 6 months ago

My linked PR only solves the issue for GDExtension, so I uploaded another MRP which showcases specifically this. I kept my DLL in there for convenience, but you may also build it from source if you do not trust random binaries off the internet.