From 2d7e772909571676dd4266337d43086bd2927e93 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 29 Mar 2021 00:06:34 -0700 Subject: UI big structure --- core/src/Utils/I18n.cpp | 53 ++++++++++++++++++++++++++++--------------------- core/src/Utils/I18n.hpp | 4 ++-- 2 files changed, 32 insertions(+), 25 deletions(-) (limited to 'core/src/Utils') diff --git a/core/src/Utils/I18n.cpp b/core/src/Utils/I18n.cpp index 645cfc5..f5a6a49 100644 --- a/core/src/Utils/I18n.cpp +++ b/core/src/Utils/I18n.cpp @@ -28,15 +28,14 @@ public: } public: - tsl::array_map localeInfos; - tsl::array_map currentEntries; - LanguageInfo* currentLanguage; + tsl::array_map LocaleInfos; + tsl::array_map CurrentEntries; + LanguageInfo* CurrentLanguage = nullptr; }; -std::string findLocalizedName(const fs::path& localeFile) { +std::string FindLocalizedName(const fs::path& localeFile) { std::ifstream ifs{ localeFile }; if (!ifs) { - // TODO log error throw std::runtime_error("Failed to open locale file."); } @@ -45,7 +44,6 @@ std::string findLocalizedName(const fs::path& localeFile) { if (auto& name = root["$localized_name"]; name.isString()) { return std::string(name.asCString()); } else { - // TODO log error throw std::runtime_error("Failed to find $localized_name in language file."); } } @@ -57,8 +55,7 @@ void I18n::Init() { auto dir = fs::current_path() / "locale"; if (!fs::exists(dir)) { - // TODO log error - return; + throw std::runtime_error("Failed to find locale directory."); } for (auto& elm : fs::directory_iterator{ dir }) { @@ -67,16 +64,14 @@ void I18n::Init() { auto& path = elm.path(); auto codeName = path.stem().string(); - state.localeInfos.emplace( + state.LocaleInfos.emplace( codeName, LanguageInfo{ .codeName = codeName, - .localeName = findLocalizedName(path), + .localeName = FindLocalizedName(path), .file = path, }); } - - SetLanguage("en_US"); } void I18n::Shutdown() { @@ -92,25 +87,35 @@ void I18n::ReloadLocales() { std::string_view I18n::GetLanguage() { auto& state = I18nState::Get(); - return state.currentLanguage->localeName; + return state.CurrentLanguage->localeName; } bool I18n::SetLanguage(std::string_view lang) { auto& state = I18nState::Get(); - if (!state.currentLanguage) return false; - if (state.currentLanguage->codeName == lang) return false; + if (state.CurrentLanguage && + state.CurrentLanguage->codeName == lang) + { + return false; + } - if (auto iter = state.localeInfos.find(lang); iter != state.localeInfos.end()) { - state.currentLanguage = &iter.value(); - state.currentEntries.clear(); + if (auto iter = state.LocaleInfos.find(lang); iter != state.LocaleInfos.end()) { + state.CurrentLanguage = &iter.value(); + state.CurrentEntries.clear(); - auto& file = state.currentLanguage->file; + auto& file = state.CurrentLanguage->file; std::ifstream ifs{ file }; Json::Value root; ifs >> root; for (auto name : root.getMemberNames()) { + if (name == "$localized_name") { + continue; + } + auto& value = root[name]; + if (value.isString()) { + state.CurrentEntries.insert(name, value.asCString()); + } } } ReloadLocales(); @@ -119,8 +124,8 @@ bool I18n::SetLanguage(std::string_view lang) { std::optional I18n::Lookup(std::string_view key) { auto& state = I18nState::Get(); - auto iter = state.currentEntries.find(key); - if (iter != state.currentEntries.end()) { + auto iter = state.CurrentEntries.find(key); + if (iter != state.CurrentEntries.end()) { return iter.value(); } else { return std::nullopt; @@ -140,10 +145,12 @@ std::string_view I18n::LookupUnwrap(std::string_view key) { } BasicTranslation::BasicTranslation(std::string_view key) - : mContent{ I18n::LookupUnwrap(key) } { + // Assuming the string is null terminated, which it is here (because we store interally using std::string) + // TODO properly use std::string_view when imgui supports it + : mContent{ I18n::LookupUnwrap(key).data() } { } -std::string_view BasicTranslation::Get() const { +const char* BasicTranslation::Get() const { return mContent; } diff --git a/core/src/Utils/I18n.hpp b/core/src/Utils/I18n.hpp index 6b72d29..b9386be 100644 --- a/core/src/Utils/I18n.hpp +++ b/core/src/Utils/I18n.hpp @@ -41,11 +41,11 @@ struct FloatArgument { class BasicTranslation { private: - std::string_view mContent; + const char* mContent; public: BasicTranslation(std::string_view key); - std::string_view Get() const; + const char* Get() const; }; class FormattedTranslation { -- cgit v1.2.3-70-g09d2