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/content/data_processor_nodes.cpp
|
||||||
|
|
||||||
source/math_evaluator.cpp
|
source/math_evaluator.cpp
|
||||||
|
|
||||||
|
source/lang/en_US.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add additional include directories here #
|
# Add additional include directories here #
|
||||||
|
@ -19,8 +19,8 @@ namespace hex::plugin::builtin {
|
|||||||
|
|
||||||
static int selection = [&]() -> int {
|
static int selection = [&]() -> int {
|
||||||
u16 index = 0;
|
u16 index = 0;
|
||||||
for (auto &[languageName, languageFile] : languages){
|
for (auto &[languageCode, languageName] : languages){
|
||||||
if (languageFile == setting)
|
if (languageCode == setting)
|
||||||
return index;
|
return index;
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ namespace hex::plugin::builtin {
|
|||||||
|
|
||||||
static auto languageNames = [&]() {
|
static auto languageNames = [&]() {
|
||||||
std::vector<const char*> result;
|
std::vector<const char*> result;
|
||||||
for (auto &[languageName, languageFile] : languages)
|
for (auto &[languageCode, languageName] : languages)
|
||||||
result.push_back(languageName.c_str());
|
result.push_back(languageName.c_str());
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -40,9 +40,9 @@ namespace hex::plugin::builtin {
|
|||||||
if (ImGui::Combo("##language", &selection, languageNames.data(), languageNames.size())) {
|
if (ImGui::Combo("##language", &selection, languageNames.data(), languageNames.size())) {
|
||||||
|
|
||||||
u16 index = 0;
|
u16 index = 0;
|
||||||
for (auto &[languageName, languageFile] : languages){
|
for (auto &[languageCode, languageName] : languages){
|
||||||
if (selection == index) {
|
if (selection == index) {
|
||||||
setting = languageFile;
|
setting = languageCode;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
index++;
|
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 registerSettings();
|
||||||
void registerDataProcessorNodes();
|
void registerDataProcessorNodes();
|
||||||
|
|
||||||
|
void registerLanguageEnUS();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IMHEX_PLUGIN_SETUP {
|
IMHEX_PLUGIN_SETUP {
|
||||||
@ -22,6 +24,7 @@ IMHEX_PLUGIN_SETUP {
|
|||||||
registerSettings();
|
registerSettings();
|
||||||
registerDataProcessorNodes();
|
registerDataProcessorNodes();
|
||||||
|
|
||||||
|
registerLanguageEnUS();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -174,6 +174,14 @@ namespace hex {
|
|||||||
private:
|
private:
|
||||||
static void add(const Entry &entry);
|
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
|
#pragma once
|
||||||
|
|
||||||
|
#include <initializer_list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
namespace hex {
|
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 {
|
class LangEntry {
|
||||||
public:
|
public:
|
||||||
LangEntry(const char *unlocalizedString);
|
LangEntry(const char *unlocalizedString);
|
||||||
|
@ -63,7 +63,10 @@ namespace hex {
|
|||||||
static u32 patternPaletteOffset;
|
static u32 patternPaletteOffset;
|
||||||
static std::string errorPopupMessage;
|
static std::string errorPopupMessage;
|
||||||
static std::list<ImHexApi::Bookmarks::Entry> bookmarkEntries;
|
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 fileBrowser;
|
||||||
static imgui_addons::ImGuiFileBrowser::DialogMode fileBrowserDialogMode;
|
static imgui_addons::ImGuiFileBrowser::DialogMode fileBrowserDialogMode;
|
||||||
|
@ -192,4 +192,22 @@ namespace hex {
|
|||||||
std::vector<ContentRegistry::DataProcessorNode::Entry>& ContentRegistry::DataProcessorNode::getEntries() {
|
std::vector<ContentRegistry::DataProcessorNode::Entry>& ContentRegistry::DataProcessorNode::getEntries() {
|
||||||
return SharedData::dataProcessorNodes;
|
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 "hex/helpers/shared_data.hpp"
|
||||||
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
namespace hex {
|
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::LangEntry(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
||||||
|
|
||||||
LangEntry::operator std::string() const {
|
LangEntry::operator std::string() const {
|
||||||
@ -23,7 +28,7 @@ namespace hex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string_view LangEntry::get() const {
|
std::string_view LangEntry::get() const {
|
||||||
auto &lang = SharedData::loadedLanguage;
|
auto &lang = SharedData::loadedLanguageStrings;
|
||||||
if (lang.find(this->m_unlocalizedString) != lang.end())
|
if (lang.find(this->m_unlocalizedString) != lang.end())
|
||||||
return lang[this->m_unlocalizedString];
|
return lang[this->m_unlocalizedString];
|
||||||
else
|
else
|
||||||
@ -31,57 +36,26 @@ namespace hex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LangEntry::loadLanguage(std::string_view language) {
|
void LangEntry::loadLanguage(std::string_view language) {
|
||||||
SharedData::loadedLanguage.clear();
|
constexpr auto DefaultLanguage = "en-US";
|
||||||
|
|
||||||
bool isDefaultLanguage = language == "en-US";
|
SharedData::loadedLanguageStrings.clear();
|
||||||
|
|
||||||
try {
|
auto &definitions = ContentRegistry::Language::getLanguageDefinitions();
|
||||||
std::ifstream languageFile("lang/" + std::string(language) + ".json");
|
|
||||||
nlohmann::json languageJson;
|
|
||||||
|
|
||||||
if (!languageFile.is_open() && !isDefaultLanguage)
|
if (!definitions.contains(language.data()))
|
||||||
languageFile.open("lang/en-US.json");
|
|
||||||
|
|
||||||
languageFile >> languageJson;
|
|
||||||
|
|
||||||
for (auto &[unlocalizedString, localizedString] : languageJson["lang"].items())
|
|
||||||
SharedData::loadedLanguage.insert({ unlocalizedString, localizedString });
|
|
||||||
|
|
||||||
if (!isDefaultLanguage) {
|
|
||||||
languageFile.open("lang/en-US.json");
|
|
||||||
if (!languageFile.good())
|
|
||||||
return;
|
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())
|
if (language != DefaultLanguage) {
|
||||||
SharedData::loadedLanguage.insert({ unlocalizedString, localizedString });
|
for (auto &definition : definitions[DefaultLanguage])
|
||||||
|
SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
||||||
}
|
}
|
||||||
} catch (std::exception &e) {
|
|
||||||
printf("Language load error: %s\n", e.what());
|
|
||||||
|
|
||||||
if (!isDefaultLanguage)
|
|
||||||
loadLanguage("en-US");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::map<std::string, std::string>& LangEntry::getSupportedLanguages() {
|
const std::map<std::string, std::string>& LangEntry::getSupportedLanguages() {
|
||||||
static std::map<std::string, std::string> languages;
|
return ContentRegistry::Language::getLanguages();
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -17,7 +17,10 @@ namespace hex {
|
|||||||
u32 SharedData::patternPaletteOffset;
|
u32 SharedData::patternPaletteOffset;
|
||||||
std::string SharedData::errorPopupMessage;
|
std::string SharedData::errorPopupMessage;
|
||||||
std::list<ImHexApi::Bookmarks::Entry> SharedData::bookmarkEntries;
|
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 SharedData::fileBrowser;
|
||||||
imgui_addons::ImGuiFileBrowser::DialogMode SharedData::fileBrowserDialogMode;
|
imgui_addons::ImGuiFileBrowser::DialogMode SharedData::fileBrowserDialogMode;
|
||||||
|
@ -200,6 +200,7 @@ namespace hex {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Window::frameBegin() {
|
void Window::frameBegin() {
|
||||||
|
printf("%s\n", static_cast<const char*>("hello.world"_lang));
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
ImGui_ImplOpenGL3_NewFrame();
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user