Revamped language system right away again to allow plugins to use it
This commit is contained in:
parent
4a8e59a95b
commit
9227fba474
@ -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 #
|
||||
|
@ -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<const char*> 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++;
|
||||
|
13
plugins/builtin/source/lang/en_US.cpp
Normal file
13
plugins/builtin/source/lang/en_US.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include <hex/plugin.hpp>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
void registerLanguageEnUS() {
|
||||
ContentRegistry::Language::registerLanguage("English (US)", "en-US");
|
||||
|
||||
ContentRegistry::Language::addLocalizations("en-US", {
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<std::string, std::string>& getLanguages();
|
||||
static std::map<std::string, std::vector<LanguageDefinition>>& getLanguageDefinitions();
|
||||
};
|
||||
};
|
||||
|
||||
}
|
@ -1,11 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <initializer_list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace hex {
|
||||
|
||||
class LanguageDefinition {
|
||||
public:
|
||||
LanguageDefinition(std::initializer_list<std::pair<std::string, std::string>> entries);
|
||||
|
||||
const std::map<std::string, std::string>& getEntries() const;
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> m_entries;
|
||||
};
|
||||
|
||||
class LangEntry {
|
||||
public:
|
||||
LangEntry(const char *unlocalizedString);
|
||||
|
@ -63,7 +63,10 @@ namespace hex {
|
||||
static u32 patternPaletteOffset;
|
||||
static std::string errorPopupMessage;
|
||||
static std::list<ImHexApi::Bookmarks::Entry> bookmarkEntries;
|
||||
static std::map<std::string, std::string> loadedLanguage;
|
||||
|
||||
static std::map<std::string, std::string> languageNames;
|
||||
static std::map<std::string, std::vector<LanguageDefinition>> languageDefinitions;
|
||||
static std::map<std::string, std::string> loadedLanguageStrings;
|
||||
|
||||
static imgui_addons::ImGuiFileBrowser fileBrowser;
|
||||
static imgui_addons::ImGuiFileBrowser::DialogMode fileBrowserDialogMode;
|
||||
|
@ -192,4 +192,22 @@ namespace hex {
|
||||
std::vector<ContentRegistry::DataProcessorNode::Entry>& 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<std::string, std::string>& ContentRegistry::Language::getLanguages() {
|
||||
return SharedData::languageNames;
|
||||
}
|
||||
|
||||
std::map<std::string, std::vector<LanguageDefinition>>& ContentRegistry::Language::getLanguageDefinitions() {
|
||||
return SharedData::languageDefinitions;
|
||||
}
|
||||
}
|
@ -2,12 +2,17 @@
|
||||
|
||||
#include "hex/helpers/shared_data.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
namespace hex {
|
||||
|
||||
LanguageDefinition::LanguageDefinition(std::initializer_list<std::pair<std::string, std::string>> entries) {
|
||||
for (auto pair : entries)
|
||||
this->m_entries.insert(pair);
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string>& 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<std::string, std::string>& LangEntry::getSupportedLanguages() {
|
||||
static std::map<std::string, std::string> 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<std::string>(), entry.path().stem().string() });
|
||||
} catch (std::exception &e) {}
|
||||
}
|
||||
}
|
||||
|
||||
return languages;
|
||||
return ContentRegistry::Language::getLanguages();
|
||||
}
|
||||
|
||||
}
|
@ -17,7 +17,10 @@ namespace hex {
|
||||
u32 SharedData::patternPaletteOffset;
|
||||
std::string SharedData::errorPopupMessage;
|
||||
std::list<ImHexApi::Bookmarks::Entry> SharedData::bookmarkEntries;
|
||||
std::map<std::string, std::string> SharedData::loadedLanguage;
|
||||
|
||||
std::map<std::string, std::string> SharedData::languageNames;
|
||||
std::map<std::string, std::vector<LanguageDefinition>> SharedData::languageDefinitions;
|
||||
std::map<std::string, std::string> SharedData::loadedLanguageStrings;
|
||||
|
||||
imgui_addons::ImGuiFileBrowser SharedData::fileBrowser;
|
||||
imgui_addons::ImGuiFileBrowser::DialogMode SharedData::fileBrowserDialogMode;
|
||||
|
@ -200,6 +200,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
void Window::frameBegin() {
|
||||
printf("%s\n", static_cast<const char*>("hello.world"_lang));
|
||||
glfwPollEvents();
|
||||
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
|
Loading…
x
Reference in New Issue
Block a user