From 077554daf5ec8a92ed68fb97020e88dbfb0ea29f Mon Sep 17 00:00:00 2001 From: Hop311 Date: Thu, 14 Sep 2023 21:07:08 +0100 Subject: Compat localisation loading --- .../openvic-extension/LoadGameCompatibility.cpp | 7 ++++- .../src/openvic-extension/LoadLocalisation.cpp | 33 ++++++++++++++++++---- .../src/openvic-extension/LoadLocalisation.hpp | 14 +++++---- 3 files changed, 43 insertions(+), 11 deletions(-) (limited to 'extension/src') diff --git a/extension/src/openvic-extension/LoadGameCompatibility.cpp b/extension/src/openvic-extension/LoadGameCompatibility.cpp index e8e3314..b696315 100644 --- a/extension/src/openvic-extension/LoadGameCompatibility.cpp +++ b/extension/src/openvic-extension/LoadGameCompatibility.cpp @@ -5,6 +5,7 @@ #include +#include "openvic-extension/LoadLocalisation.hpp" #include "openvic-extension/Utilities.hpp" using namespace godot; @@ -83,7 +84,6 @@ Error GameSingleton::load_defines_compatibility_mode(PackedStringArray const& fi err = FAILED; } - game_manager.map.lock_regions(); if (_load_terrain_variants_compatibility_mode( std_to_godot_string(dataloader.lookup_file(terrain_image_file).string()), std_to_godot_string(dataloader.lookup_file(terrain_texture_file).string()) @@ -102,6 +102,11 @@ Error GameSingleton::load_defines_compatibility_mode(PackedStringArray const& fi UtilityFunctions::push_error("Failed to hardcoded defines!"); err = FAILED; } + if (!dataloader.load_localisation_files(LoadLocalisation::add_message)) { + UtilityFunctions::push_error("Failed to load localisation!"); + err = FAILED; + } + return err; } diff --git a/extension/src/openvic-extension/LoadLocalisation.cpp b/extension/src/openvic-extension/LoadLocalisation.cpp index dc7702c..9ab7298 100644 --- a/extension/src/openvic-extension/LoadLocalisation.cpp +++ b/extension/src/openvic-extension/LoadLocalisation.cpp @@ -5,6 +5,8 @@ #include #include +#include "openvic-extension/Utilities.hpp" + using namespace godot; using namespace OpenVic; @@ -30,7 +32,7 @@ LoadLocalisation::~LoadLocalisation() { singleton = nullptr; } -Error LoadLocalisation::_load_file_into_translation(String const& file_path, Ref translation) { +Error LoadLocalisation::_load_file_into_translation(String const& file_path, Ref translation) const { const Ref file = FileAccess::open(file_path, FileAccess::ModeFlags::READ); Error err = FileAccess::get_open_error(); if (err != OK || file.is_null()) { @@ -57,7 +59,7 @@ Error LoadLocalisation::_load_file_into_translation(String const& file_path, Ref return err; } -Ref LoadLocalisation::_get_translation(String const& locale) { +Ref LoadLocalisation::_get_translation(String const& locale) const { TranslationServer* server = TranslationServer::get_singleton(); if (server == nullptr) { UtilityFunctions::push_error("Failed to get TranslationServer singleton"); @@ -72,14 +74,14 @@ Ref LoadLocalisation::_get_translation(String const& locale) { return translation; } -Error LoadLocalisation::load_file(String const& file_path, String const& locale) { +Error LoadLocalisation::load_file(String const& file_path, String const& locale) const { return _load_file_into_translation(file_path, _get_translation(locale)); } /* REQUIREMENTS * FS-18, FS-24, FS-25 */ -Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& locale) { +Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& locale) const { if (!DirAccess::dir_exists_absolute(dir_path)) { UtilityFunctions::push_error("Locale directory does not exist: ", dir_path); return FAILED; @@ -109,7 +111,7 @@ Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& lo /* REQUIREMENTS * FS-23 */ -Error LoadLocalisation::load_localisation_dir(String const& dir_path) { +Error LoadLocalisation::load_localisation_dir(String const& dir_path) const { if (!DirAccess::dir_exists_absolute(dir_path)) { UtilityFunctions::push_error("Localisation directory does not exist: ", dir_path); return FAILED; @@ -134,3 +136,24 @@ Error LoadLocalisation::load_localisation_dir(String const& dir_path) { } return err; } +bool LoadLocalisation::add_message(std::string_view key, Dataloader::locale_t locale, std::string_view localisation) { + static Ref translations[Dataloader::_LocaleCount] = { nullptr }; + Ref& translation = translations[locale]; + if (translation.is_null()) { + translation = singleton->_get_translation(Dataloader::locale_names[locale]); + if (translation.is_null()) { + UtilityFunctions::push_error("Failed to get translation object: ", Dataloader::locale_names[locale]); + return false; + } + } + const StringName godot_key = std_to_godot_string(std::string { key }); + const StringName godot_localisation = std_to_godot_string(std::string { localisation }); + if (0) { + const StringName old_localisation = translation->get_message(godot_key); + if (!old_localisation.is_empty()) { + UtilityFunctions::push_warning("Changing translation ", godot_key, " (", Dataloader::locale_names[locale], ") from \"", old_localisation, "\" to \"", godot_localisation, "\""); + } + } + translation->add_message(godot_key, godot_localisation); + return true; +} diff --git a/extension/src/openvic-extension/LoadLocalisation.hpp b/extension/src/openvic-extension/LoadLocalisation.hpp index 04ec5c7..4f09ef3 100644 --- a/extension/src/openvic-extension/LoadLocalisation.hpp +++ b/extension/src/openvic-extension/LoadLocalisation.hpp @@ -2,6 +2,8 @@ #include +#include + namespace OpenVic { class LoadLocalisation : public godot::Object { @@ -9,8 +11,8 @@ namespace OpenVic { static LoadLocalisation* singleton; - godot::Error _load_file_into_translation(godot::String const& file_path, godot::Ref translation); - godot::Ref _get_translation(godot::String const& locale); + godot::Error _load_file_into_translation(godot::String const& file_path, godot::Ref translation) const; + godot::Ref _get_translation(godot::String const& locale) const; protected: static void _bind_methods(); @@ -21,8 +23,10 @@ namespace OpenVic { LoadLocalisation(); ~LoadLocalisation(); - godot::Error load_file(godot::String const& file_path, godot::String const& locale); - godot::Error load_locale_dir(godot::String const& dir_path, godot::String const& locale); - godot::Error load_localisation_dir(godot::String const& dir_path); + godot::Error load_file(godot::String const& file_path, godot::String const& locale) const; + godot::Error load_locale_dir(godot::String const& dir_path, godot::String const& locale) const; + godot::Error load_localisation_dir(godot::String const& dir_path) const; + + static bool add_message(std::string_view key, Dataloader::locale_t locale, std::string_view localisation); }; } -- cgit v1.2.3-56-ga3b1