From b3a8d02d19c41e172d1e7b9e06a442b059d3b262 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 23 Jan 2022 22:08:19 +0100 Subject: [PATCH] lang: Don't hardcode fallback language anymore --- lib/libimhex/include/hex/helpers/lang.hpp | 9 +++++-- lib/libimhex/source/helpers/lang.cpp | 31 +++++++++++++++-------- plugins/builtin/source/lang/en_US.cpp | 1 + 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/lang.hpp b/lib/libimhex/include/hex/helpers/lang.hpp index 36048467d..5ca8e1f7b 100644 --- a/lib/libimhex/include/hex/helpers/lang.hpp +++ b/lib/libimhex/include/hex/helpers/lang.hpp @@ -27,13 +27,18 @@ namespace hex { operator std::string_view() const; operator const char*() const; - [[nodiscard]] std::string_view get() const; + [[nodiscard]] const std::string& get() const; - static void loadLanguage(std::string_view language); + static void loadLanguage(const std::string &language); static const std::map& getSupportedLanguages(); + static void setFallbackLanguage(const std::string &language); + static const std::string& getFallbackLanguage(); + private: std::string m_unlocalizedString; + + static std::string s_fallbackLanguage; }; std::string operator+(const std::string &&left, const LangEntry &&right); diff --git a/lib/libimhex/source/helpers/lang.cpp b/lib/libimhex/source/helpers/lang.cpp index 62acd74a7..9ddc557f5 100644 --- a/lib/libimhex/source/helpers/lang.cpp +++ b/lib/libimhex/source/helpers/lang.cpp @@ -4,6 +4,8 @@ namespace hex { + std::string LangEntry::s_fallbackLanguage; + LanguageDefinition::LanguageDefinition(std::initializer_list> entries) { for (auto pair : entries) this->m_entries.insert(pair); @@ -18,7 +20,7 @@ namespace hex { LangEntry::LangEntry(std::string_view unlocalizedString) : m_unlocalizedString(unlocalizedString) { } LangEntry::operator std::string() const { - return std::string(get()); + return get(); } LangEntry::operator std::string_view() const { @@ -26,7 +28,7 @@ namespace hex { } LangEntry::operator const char*() const { - return get().data(); + return get().c_str(); } std::string operator+(const std::string &&left, const LangEntry &&right) { @@ -57,29 +59,28 @@ namespace hex { return static_cast(left) + right; } - std::string_view LangEntry::get() const { + const std::string& LangEntry::get() const { auto &lang = SharedData::loadedLanguageStrings; - if (lang.find(this->m_unlocalizedString) != lang.end()) + if (lang.contains(this->m_unlocalizedString)) return lang[this->m_unlocalizedString]; else return this->m_unlocalizedString; } - void LangEntry::loadLanguage(std::string_view language) { - constexpr auto DefaultLanguage = "en-US"; - + void LangEntry::loadLanguage(const std::string &language) { SharedData::loadedLanguageStrings.clear(); auto &definitions = ContentRegistry::Language::getLanguageDefinitions(); - if (!definitions.contains(language.data())) + if (!definitions.contains(language)) return; - for (auto &definition : definitions[language.data()]) + for (auto &definition : definitions[language]) SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); - if (language != DefaultLanguage) { - for (auto &definition : definitions[DefaultLanguage]) + const auto fallbackLanguage = LangEntry::getFallbackLanguage(); + if (language != fallbackLanguage) { + for (auto &definition : definitions[fallbackLanguage]) SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); } } @@ -88,4 +89,12 @@ namespace hex { return ContentRegistry::Language::getLanguages(); } + void LangEntry::setFallbackLanguage(const std::string &language) { + LangEntry::s_fallbackLanguage = language; + } + + const std::string& LangEntry::getFallbackLanguage() { + return LangEntry::s_fallbackLanguage; + } + } \ No newline at end of file diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp index 04c1c26f7..18f932423 100644 --- a/plugins/builtin/source/lang/en_US.cpp +++ b/plugins/builtin/source/lang/en_US.cpp @@ -5,6 +5,7 @@ namespace hex::plugin::builtin { void registerLanguageEnUS() { ContentRegistry::Language::registerLanguage("English (US)", "en-US"); + LangEntry::setFallbackLanguage("en-US"); ContentRegistry::Language::addLocalizations("en-US", { /* ImHex default functionality */