mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-03 05:57:40 -05:00
refactor: Rename and update localization system
This commit is contained in:
@@ -267,13 +267,13 @@ namespace hex {
|
||||
preview = this->m_items[this->m_value].c_str();
|
||||
|
||||
bool changed = false;
|
||||
if (ImGui::BeginCombo(name.c_str(), LangEntry(preview))) {
|
||||
if (ImGui::BeginCombo(name.c_str(), Lang(preview))) {
|
||||
|
||||
int index = 0;
|
||||
for (const auto &item : this->m_items) {
|
||||
const bool selected = index == this->m_value;
|
||||
|
||||
if (ImGui::Selectable(LangEntry(item), selected)) {
|
||||
if (ImGui::Selectable(Lang(item), selected)) {
|
||||
this->m_value = index;
|
||||
changed = true;
|
||||
}
|
||||
@@ -665,7 +665,7 @@ namespace hex {
|
||||
const auto &fallback = data["fallback"];
|
||||
|
||||
if (fallback.is_boolean() && fallback.get<bool>())
|
||||
LangEntry::setFallbackLanguage(code.get<std::string>());
|
||||
LocalizationManager::impl::setFallbackLanguage(code.get<std::string>());
|
||||
}
|
||||
|
||||
impl::getLanguages().insert({ code.get<std::string>(), hex::format("{} ({})", language.get<std::string>(), country.get<std::string>()) });
|
||||
@@ -691,8 +691,8 @@ namespace hex {
|
||||
return languages;
|
||||
}
|
||||
|
||||
std::map<std::string, std::vector<LanguageDefinition>> &getLanguageDefinitions() {
|
||||
static std::map<std::string, std::vector<LanguageDefinition>> definitions;
|
||||
std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>> &getLanguageDefinitions() {
|
||||
static std::map<std::string, std::vector<LocalizationManager::LanguageDefinition>> definitions;
|
||||
|
||||
return definitions;
|
||||
}
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
#include <hex/api/localization.hpp>
|
||||
|
||||
#include <hex/api/content_registry.hpp>
|
||||
|
||||
namespace hex {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string s_fallbackLanguage;
|
||||
std::string s_selectedLanguage;
|
||||
std::map<std::string, std::string> s_currStrings;
|
||||
|
||||
}
|
||||
|
||||
|
||||
LanguageDefinition::LanguageDefinition(std::map<std::string, std::string> &&entries) {
|
||||
for (const auto &[key, value] : entries) {
|
||||
if (value.empty())
|
||||
continue;
|
||||
|
||||
this->m_entries.insert({ key, value });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string> &LanguageDefinition::getEntries() const {
|
||||
return this->m_entries;
|
||||
}
|
||||
|
||||
LangEntry::LangEntry(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
||||
LangEntry::LangEntry(std::string unlocalizedString) : m_unlocalizedString(std::move(unlocalizedString)) { }
|
||||
LangEntry::LangEntry(std::string_view unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
||||
|
||||
LangEntry::operator std::string() const {
|
||||
return get();
|
||||
}
|
||||
|
||||
LangEntry::operator std::string_view() const {
|
||||
return get();
|
||||
}
|
||||
|
||||
LangEntry::operator const char *() const {
|
||||
return get().c_str();
|
||||
}
|
||||
|
||||
std::string operator+(const std::string &&left, const LangEntry &&right) {
|
||||
return left + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const LangEntry &&left, const std::string &&right) {
|
||||
return static_cast<std::string>(left) + right;
|
||||
}
|
||||
|
||||
std::string operator+(const LangEntry &&left, const LangEntry &&right) {
|
||||
return static_cast<std::string>(left) + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const std::string_view &&left, const LangEntry &&right) {
|
||||
return std::string(left) + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const LangEntry &&left, const std::string_view &&right) {
|
||||
return static_cast<std::string>(left) + std::string(right);
|
||||
}
|
||||
|
||||
std::string operator+(const char *left, const LangEntry &&right) {
|
||||
return left + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const LangEntry &&left, const char *right) {
|
||||
return static_cast<std::string>(left) + right;
|
||||
}
|
||||
|
||||
const std::string &LangEntry::get() const {
|
||||
auto &lang = s_currStrings;
|
||||
if (lang.contains(this->m_unlocalizedString))
|
||||
return lang[this->m_unlocalizedString];
|
||||
else
|
||||
return this->m_unlocalizedString;
|
||||
}
|
||||
|
||||
void LangEntry::loadLanguage(const std::string &language) {
|
||||
s_currStrings.clear();
|
||||
|
||||
auto &definitions = ContentRegistry::Language::impl::getLanguageDefinitions();
|
||||
|
||||
if (!definitions.contains(language))
|
||||
return;
|
||||
|
||||
for (auto &definition : definitions[language])
|
||||
s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
||||
|
||||
const auto fallbackLanguage = LangEntry::getFallbackLanguage();
|
||||
if (language != fallbackLanguage) {
|
||||
for (auto &definition : definitions[fallbackLanguage])
|
||||
s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
||||
}
|
||||
|
||||
s_selectedLanguage = language;
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string> &LangEntry::getSupportedLanguages() {
|
||||
return ContentRegistry::Language::impl::getLanguages();
|
||||
}
|
||||
|
||||
void LangEntry::setFallbackLanguage(const std::string &language) {
|
||||
s_fallbackLanguage = language;
|
||||
}
|
||||
|
||||
const std::string &LangEntry::getFallbackLanguage() {
|
||||
return s_fallbackLanguage;
|
||||
}
|
||||
|
||||
void LangEntry::resetLanguageStrings() {
|
||||
s_currStrings.clear();
|
||||
s_selectedLanguage.clear();
|
||||
}
|
||||
|
||||
const std::string &LangEntry::getSelectedLanguage() {
|
||||
return s_selectedLanguage;
|
||||
}
|
||||
|
||||
}
|
||||
129
lib/libimhex/source/api/localization_manager.cpp
Normal file
129
lib/libimhex/source/api/localization_manager.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
#include <hex/api/content_registry.hpp>
|
||||
#include <hex/api/localization_manager.hpp>
|
||||
|
||||
namespace hex {
|
||||
|
||||
namespace LocalizationManager {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string s_fallbackLanguage;
|
||||
std::string s_selectedLanguage;
|
||||
std::map<std::string, std::string> s_currStrings;
|
||||
|
||||
}
|
||||
|
||||
namespace impl {
|
||||
|
||||
void resetLanguageStrings() {
|
||||
s_currStrings.clear();
|
||||
s_selectedLanguage.clear();
|
||||
}
|
||||
|
||||
void setFallbackLanguage(const std::string &language) {
|
||||
s_fallbackLanguage = language;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
LanguageDefinition::LanguageDefinition(std::map<std::string, std::string> &&entries) {
|
||||
for (const auto &[key, value] : entries) {
|
||||
if (value.empty())
|
||||
continue;
|
||||
|
||||
this->m_entries.insert({ key, value });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string> &LanguageDefinition::getEntries() const {
|
||||
return this->m_entries;
|
||||
}
|
||||
|
||||
void loadLanguage(const std::string &language) {
|
||||
s_currStrings.clear();
|
||||
|
||||
auto &definitions = ContentRegistry::Language::impl::getLanguageDefinitions();
|
||||
|
||||
if (!definitions.contains(language))
|
||||
return;
|
||||
|
||||
for (auto &definition : definitions[language])
|
||||
s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
||||
|
||||
const auto& fallbackLanguage = getFallbackLanguage();
|
||||
if (language != fallbackLanguage) {
|
||||
for (auto &definition : definitions[fallbackLanguage])
|
||||
s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
||||
}
|
||||
|
||||
s_selectedLanguage = language;
|
||||
}
|
||||
|
||||
const std::map<std::string, std::string> &getSupportedLanguages() {
|
||||
return ContentRegistry::Language::impl::getLanguages();
|
||||
}
|
||||
|
||||
const std::string &getFallbackLanguage() {
|
||||
return s_fallbackLanguage;
|
||||
}
|
||||
|
||||
const std::string &getSelectedLanguage() {
|
||||
return s_selectedLanguage;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Lang::Lang(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
||||
Lang::Lang(std::string unlocalizedString) : m_unlocalizedString(std::move(unlocalizedString)) { }
|
||||
Lang::Lang(std::string_view unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
||||
|
||||
Lang::operator std::string() const {
|
||||
return get();
|
||||
}
|
||||
|
||||
Lang::operator std::string_view() const {
|
||||
return get();
|
||||
}
|
||||
|
||||
Lang::operator const char *() const {
|
||||
return get().c_str();
|
||||
}
|
||||
|
||||
std::string operator+(const std::string &&left, const Lang &&right) {
|
||||
return left + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const Lang &&left, const std::string &&right) {
|
||||
return static_cast<std::string>(left) + right;
|
||||
}
|
||||
|
||||
std::string operator+(const Lang &&left, const Lang &&right) {
|
||||
return static_cast<std::string>(left) + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const std::string_view &&left, const Lang &&right) {
|
||||
return std::string(left) + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const Lang &&left, const std::string_view &&right) {
|
||||
return static_cast<std::string>(left) + std::string(right);
|
||||
}
|
||||
|
||||
std::string operator+(const char *left, const Lang &&right) {
|
||||
return left + static_cast<std::string>(right);
|
||||
}
|
||||
|
||||
std::string operator+(const Lang &&left, const char *right) {
|
||||
return static_cast<std::string>(left) + right;
|
||||
}
|
||||
|
||||
const std::string &Lang::get() const {
|
||||
auto &lang = LocalizationManager::s_currStrings;
|
||||
if (lang.contains(this->m_unlocalizedString))
|
||||
return lang[this->m_unlocalizedString];
|
||||
else
|
||||
return this->m_unlocalizedString;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <hex/api/task_manager.hpp>
|
||||
|
||||
#include <hex/api/localization.hpp>
|
||||
#include <hex/api/localization_manager.hpp>
|
||||
#include <hex/helpers/logger.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -249,7 +249,7 @@ namespace hex {
|
||||
}
|
||||
|
||||
try {
|
||||
setThreadName(LangEntry(task->m_unlocalizedName));
|
||||
setThreadName(Lang(task->m_unlocalizedName));
|
||||
task->m_function(*task);
|
||||
setThreadName("Idle Task");
|
||||
log::debug("Finished task {}", task->m_unlocalizedName);
|
||||
|
||||
Reference in New Issue
Block a user