1
0
mirror of synced 2024-11-25 00:00:27 +01:00
ImHex/lib/libimhex/source/api/localization.cpp
WerWolv 1991afb87b
sys: Get rid of SharedData struct and cleanup code structure (#411)
* sys: Initial refactoring of the SharedData class

* sys/pattern: More refactoring, make every provider have its own patterns

* sys: Finished up refactoring. No more SharedData!

* sys: Fixed compile on Unix

* tests: Fixed unit tests

* sys: Moved view and lang files

* pattern: Added assignment operator support to for loops

* tests: Fixed compile issue
2022-02-01 18:09:40 +01:00

105 lines
3.5 KiB
C++

#include <hex/api/localization.hpp>
#include <hex/api/content_registry.hpp>
namespace hex {
std::string LangEntry::s_fallbackLanguage;
std::map<std::string, std::string> LangEntry::s_currStrings;
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 std::string &unlocalizedString) : m_unlocalizedString(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 = LangEntry::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) {
LangEntry::s_currStrings.clear();
auto &definitions = ContentRegistry::Language::getLanguageDefinitions();
if (!definitions.contains(language))
return;
for (auto &definition : definitions[language])
LangEntry::s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
const auto fallbackLanguage = LangEntry::getFallbackLanguage();
if (language != fallbackLanguage) {
for (auto &definition : definitions[fallbackLanguage])
LangEntry::s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
}
}
const std::map<std::string, std::string> &LangEntry::getSupportedLanguages() {
return ContentRegistry::Language::getLanguages();
}
void LangEntry::setFallbackLanguage(const std::string &language) {
LangEntry::s_fallbackLanguage = language;
}
const std::string &LangEntry::getFallbackLanguage() {
return LangEntry::s_fallbackLanguage;
}
void LangEntry::resetLanguageStrings() {
LangEntry::s_currStrings.clear();
}
}