diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index 200edc136..563770801 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -14,6 +14,8 @@ add_library(${PROJECT_NAME} SHARED source/content/data_processor_nodes.cpp source/math_evaluator.cpp + + source/lang/en_US.cpp ) # Add additional include directories here # diff --git a/plugins/builtin/source/content/settings_entries.cpp b/plugins/builtin/source/content/settings_entries.cpp index f98b67ae6..4aa02caac 100644 --- a/plugins/builtin/source/content/settings_entries.cpp +++ b/plugins/builtin/source/content/settings_entries.cpp @@ -19,8 +19,8 @@ namespace hex::plugin::builtin { static int selection = [&]() -> int { u16 index = 0; - for (auto &[languageName, languageFile] : languages){ - if (languageFile == setting) + for (auto &[languageCode, languageName] : languages){ + if (languageCode == setting) return index; index++; } @@ -30,7 +30,7 @@ namespace hex::plugin::builtin { static auto languageNames = [&]() { std::vector result; - for (auto &[languageName, languageFile] : languages) + for (auto &[languageCode, languageName] : languages) result.push_back(languageName.c_str()); return result; @@ -40,9 +40,9 @@ namespace hex::plugin::builtin { if (ImGui::Combo("##language", &selection, languageNames.data(), languageNames.size())) { u16 index = 0; - for (auto &[languageName, languageFile] : languages){ + for (auto &[languageCode, languageName] : languages){ if (selection == index) { - setting = languageFile; + setting = languageCode; break; } index++; diff --git a/plugins/builtin/source/lang/en_US.cpp b/plugins/builtin/source/lang/en_US.cpp new file mode 100644 index 000000000..5917203ca --- /dev/null +++ b/plugins/builtin/source/lang/en_US.cpp @@ -0,0 +1,13 @@ +#include + +namespace hex::plugin::builtin { + + void registerLanguageEnUS() { + ContentRegistry::Language::registerLanguage("English (US)", "en-US"); + + ContentRegistry::Language::addLocalizations("en-US", { + + }); + } + +} \ No newline at end of file diff --git a/plugins/builtin/source/plugin_builtin.cpp b/plugins/builtin/source/plugin_builtin.cpp index 6ede231cc..eab411f4b 100644 --- a/plugins/builtin/source/plugin_builtin.cpp +++ b/plugins/builtin/source/plugin_builtin.cpp @@ -9,6 +9,8 @@ namespace hex::plugin::builtin { void registerSettings(); void registerDataProcessorNodes(); + void registerLanguageEnUS(); + } IMHEX_PLUGIN_SETUP { @@ -22,6 +24,7 @@ IMHEX_PLUGIN_SETUP { registerSettings(); registerDataProcessorNodes(); + registerLanguageEnUS(); } diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index a4a5b7d09..86ec1592c 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -174,6 +174,14 @@ namespace hex { private: static void add(const Entry &entry); }; + + struct Language { + static void registerLanguage(std::string_view name, std::string_view languageCode); + static void addLocalizations(std::string_view languageCode, const LanguageDefinition &definition); + + static std::map& getLanguages(); + static std::map>& getLanguageDefinitions(); + }; }; } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/helpers/lang.hpp b/plugins/libimhex/include/hex/helpers/lang.hpp index b06be0b97..79fc0a879 100644 --- a/plugins/libimhex/include/hex/helpers/lang.hpp +++ b/plugins/libimhex/include/hex/helpers/lang.hpp @@ -1,11 +1,22 @@ #pragma once +#include #include #include #include namespace hex { + class LanguageDefinition { + public: + LanguageDefinition(std::initializer_list> entries); + + const std::map& getEntries() const; + + private: + std::map m_entries; + }; + class LangEntry { public: LangEntry(const char *unlocalizedString); diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index b4d23a2ef..ea912c5e8 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -63,7 +63,10 @@ namespace hex { static u32 patternPaletteOffset; static std::string errorPopupMessage; static std::list bookmarkEntries; - static std::map loadedLanguage; + + static std::map languageNames; + static std::map> languageDefinitions; + static std::map loadedLanguageStrings; static imgui_addons::ImGuiFileBrowser fileBrowser; static imgui_addons::ImGuiFileBrowser::DialogMode fileBrowserDialogMode; diff --git a/plugins/libimhex/source/api/content_registry.cpp b/plugins/libimhex/source/api/content_registry.cpp index 31290265b..09d3d514f 100644 --- a/plugins/libimhex/source/api/content_registry.cpp +++ b/plugins/libimhex/source/api/content_registry.cpp @@ -192,4 +192,22 @@ namespace hex { std::vector& ContentRegistry::DataProcessorNode::getEntries() { return SharedData::dataProcessorNodes; } + + /* Languages */ + + void ContentRegistry::Language::registerLanguage(std::string_view name, std::string_view languageCode) { + getLanguages().insert({ languageCode.data(), name.data() }); + } + + void ContentRegistry::Language::addLocalizations(std::string_view languageCode, const LanguageDefinition &definition) { + getLanguageDefinitions()[languageCode.data()].push_back(definition); + } + + std::map& ContentRegistry::Language::getLanguages() { + return SharedData::languageNames; + } + + std::map>& ContentRegistry::Language::getLanguageDefinitions() { + return SharedData::languageDefinitions; + } } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/lang.cpp b/plugins/libimhex/source/helpers/lang.cpp index 43f8c55aa..bac42d263 100644 --- a/plugins/libimhex/source/helpers/lang.cpp +++ b/plugins/libimhex/source/helpers/lang.cpp @@ -2,12 +2,17 @@ #include "hex/helpers/shared_data.hpp" -#include -#include -#include - namespace hex { + LanguageDefinition::LanguageDefinition(std::initializer_list> entries) { + for (auto pair : entries) + this->m_entries.insert(pair); + } + + const std::map& LanguageDefinition::getEntries() const { + return this->m_entries; + } + LangEntry::LangEntry(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { } LangEntry::operator std::string() const { @@ -23,7 +28,7 @@ namespace hex { } std::string_view LangEntry::get() const { - auto &lang = SharedData::loadedLanguage; + auto &lang = SharedData::loadedLanguageStrings; if (lang.find(this->m_unlocalizedString) != lang.end()) return lang[this->m_unlocalizedString]; else @@ -31,57 +36,26 @@ namespace hex { } void LangEntry::loadLanguage(std::string_view language) { - SharedData::loadedLanguage.clear(); + constexpr auto DefaultLanguage = "en-US"; - bool isDefaultLanguage = language == "en-US"; + SharedData::loadedLanguageStrings.clear(); - try { - std::ifstream languageFile("lang/" + std::string(language) + ".json"); - nlohmann::json languageJson; + auto &definitions = ContentRegistry::Language::getLanguageDefinitions(); - if (!languageFile.is_open() && !isDefaultLanguage) - languageFile.open("lang/en-US.json"); + if (!definitions.contains(language.data())) + return; - languageFile >> languageJson; + for (auto &definition : definitions[language.data()]) + SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); - for (auto &[unlocalizedString, localizedString] : languageJson["lang"].items()) - SharedData::loadedLanguage.insert({ unlocalizedString, localizedString }); - - if (!isDefaultLanguage) { - languageFile.open("lang/en-US.json"); - if (!languageFile.good()) - return; - - languageFile >> languageJson; - - for (auto &[unlocalizedString, localizedString] : languageJson["lang"].items()) - SharedData::loadedLanguage.insert({ unlocalizedString, localizedString }); - } - } catch (std::exception &e) { - printf("Language load error: %s\n", e.what()); - - if (!isDefaultLanguage) - loadLanguage("en-US"); + if (language != DefaultLanguage) { + for (auto &definition : definitions[DefaultLanguage]) + SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end()); } - } const std::map& LangEntry::getSupportedLanguages() { - static std::map languages; - - if (languages.empty()) { - for (auto &entry : std::filesystem::directory_iterator("lang")) { - try { - std::ifstream file(entry.path()); - nlohmann::json json; - file >> json; - - languages.insert({ json["name"].get(), entry.path().stem().string() }); - } catch (std::exception &e) {} - } - } - - return languages; + return ContentRegistry::Language::getLanguages(); } } \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index 13859eaf2..01270c87d 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -17,7 +17,10 @@ namespace hex { u32 SharedData::patternPaletteOffset; std::string SharedData::errorPopupMessage; std::list SharedData::bookmarkEntries; - std::map SharedData::loadedLanguage; + + std::map SharedData::languageNames; + std::map> SharedData::languageDefinitions; + std::map SharedData::loadedLanguageStrings; imgui_addons::ImGuiFileBrowser SharedData::fileBrowser; imgui_addons::ImGuiFileBrowser::DialogMode SharedData::fileBrowserDialogMode; diff --git a/source/window.cpp b/source/window.cpp index 74202b68c..becc3afec 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -200,6 +200,7 @@ namespace hex { } void Window::frameBegin() { + printf("%s\n", static_cast("hello.world"_lang)); glfwPollEvents(); ImGui_ImplOpenGL3_NewFrame();