#include #include namespace hex { namespace { std::string s_fallbackLanguage; std::string s_selectedLanguage; std::map s_currStrings; } LanguageDefinition::LanguageDefinition(std::map &&entries) { for (const auto &[key, value] : entries) { if (value.empty()) continue; this->m_entries.insert({ key, value }); } } const std::map &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(right); } std::string operator+(const LangEntry &&left, const std::string &&right) { return static_cast(left) + right; } std::string operator+(const LangEntry &&left, const LangEntry &&right) { return static_cast(left) + static_cast(right); } std::string operator+(const std::string_view &&left, const LangEntry &&right) { return std::string(left) + static_cast(right); } std::string operator+(const LangEntry &&left, const std::string_view &&right) { return static_cast(left) + std::string(right); } std::string operator+(const char *left, const LangEntry &&right) { return left + static_cast(right); } std::string operator+(const LangEntry &&left, const char *right) { return static_cast(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 &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; } }