aboutsummaryrefslogtreecommitdiff
path: root/extension/src/openvic-extension
diff options
context:
space:
mode:
author Hop311 <hop3114@gmail.com>2023-09-14 22:07:08 +0200
committer Hop311 <hop3114@gmail.com>2023-09-14 22:20:24 +0200
commit077554daf5ec8a92ed68fb97020e88dbfb0ea29f (patch)
treeeab610624f893dec5b93977697fa0b323cfb45a3 /extension/src/openvic-extension
parentd56941e60016bf73587c63a36ac5ca9da3f6023e (diff)
Compat localisation loading
Diffstat (limited to 'extension/src/openvic-extension')
-rw-r--r--extension/src/openvic-extension/LoadGameCompatibility.cpp7
-rw-r--r--extension/src/openvic-extension/LoadLocalisation.cpp33
-rw-r--r--extension/src/openvic-extension/LoadLocalisation.hpp14
3 files changed, 43 insertions, 11 deletions
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 <openvic-simulation/utility/BMP.hpp>
+#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 <godot_cpp/classes/translation_server.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
+#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> translation) {
+Error LoadLocalisation::_load_file_into_translation(String const& file_path, Ref<Translation> translation) const {
const Ref<FileAccess> 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<Translation> LoadLocalisation::_get_translation(String const& locale) {
+Ref<Translation> 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<Translation> 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<Translation> translations[Dataloader::_LocaleCount] = { nullptr };
+ Ref<Translation>& 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 <godot_cpp/classes/translation.hpp>
+#include <openvic-simulation/dataloader/Dataloader.hpp>
+
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<godot::Translation> translation);
- godot::Ref<godot::Translation> _get_translation(godot::String const& locale);
+ godot::Error _load_file_into_translation(godot::String const& file_path, godot::Ref<godot::Translation> translation) const;
+ godot::Ref<godot::Translation> _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);
};
}