2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/localization.hpp>
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2021-02-10 18:17:09 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2022-01-23 22:08:19 +01:00
|
|
|
std::string LangEntry::s_fallbackLanguage;
|
2022-02-01 18:09:40 +01:00
|
|
|
std::map<std::string, std::string> LangEntry::s_currStrings;
|
2022-01-23 22:08:19 +01:00
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
LanguageDefinition::LanguageDefinition(std::initializer_list<std::pair<std::string, std::string>> entries) {
|
2022-03-04 20:52:39 +01:00
|
|
|
for (const auto &pair : entries)
|
2021-02-11 00:35:30 +01:00
|
|
|
this->m_entries.insert(pair);
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::map<std::string, std::string> &LanguageDefinition::getEntries() const {
|
2021-02-11 00:35:30 +01:00
|
|
|
return this->m_entries;
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:17:09 +01:00
|
|
|
LangEntry::LangEntry(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
2022-03-04 20:52:39 +01:00
|
|
|
LangEntry::LangEntry(std::string unlocalizedString) : m_unlocalizedString(std::move(unlocalizedString)) { }
|
2021-02-13 15:15:32 +01:00
|
|
|
LangEntry::LangEntry(std::string_view unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
2021-02-10 18:17:09 +01:00
|
|
|
|
|
|
|
LangEntry::operator std::string() const {
|
2022-01-23 22:08:19 +01:00
|
|
|
return get();
|
2021-02-10 18:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LangEntry::operator std::string_view() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
LangEntry::operator const char *() const {
|
2022-01-23 22:08:19 +01:00
|
|
|
return get().c_str();
|
2021-02-10 18:17:09 +01:00
|
|
|
}
|
|
|
|
|
2021-02-11 23:09:45 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::string &LangEntry::get() const {
|
2022-02-01 18:09:40 +01:00
|
|
|
auto &lang = LangEntry::s_currStrings;
|
2022-01-23 22:08:19 +01:00
|
|
|
if (lang.contains(this->m_unlocalizedString))
|
2021-02-10 18:17:09 +01:00
|
|
|
return lang[this->m_unlocalizedString];
|
|
|
|
else
|
|
|
|
return this->m_unlocalizedString;
|
|
|
|
}
|
|
|
|
|
2022-01-23 22:08:19 +01:00
|
|
|
void LangEntry::loadLanguage(const std::string &language) {
|
2022-02-01 18:09:40 +01:00
|
|
|
LangEntry::s_currStrings.clear();
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
auto &definitions = ContentRegistry::Language::getLanguageDefinitions();
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2022-01-23 22:08:19 +01:00
|
|
|
if (!definitions.contains(language))
|
2021-02-11 00:35:30 +01:00
|
|
|
return;
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2022-01-23 22:08:19 +01:00
|
|
|
for (auto &definition : definitions[language])
|
2022-02-01 18:09:40 +01:00
|
|
|
LangEntry::s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2022-01-23 22:08:19 +01:00
|
|
|
const auto fallbackLanguage = LangEntry::getFallbackLanguage();
|
|
|
|
if (language != fallbackLanguage) {
|
|
|
|
for (auto &definition : definitions[fallbackLanguage])
|
2022-02-01 18:09:40 +01:00
|
|
|
LangEntry::s_currStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
2021-02-10 18:17:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::map<std::string, std::string> &LangEntry::getSupportedLanguages() {
|
2021-02-11 00:35:30 +01:00
|
|
|
return ContentRegistry::Language::getLanguages();
|
2021-02-10 18:17:09 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 22:08:19 +01:00
|
|
|
void LangEntry::setFallbackLanguage(const std::string &language) {
|
|
|
|
LangEntry::s_fallbackLanguage = language;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
const std::string &LangEntry::getFallbackLanguage() {
|
2022-01-23 22:08:19 +01:00
|
|
|
return LangEntry::s_fallbackLanguage;
|
|
|
|
}
|
|
|
|
|
2022-02-01 18:09:40 +01:00
|
|
|
void LangEntry::resetLanguageStrings() {
|
|
|
|
LangEntry::s_currStrings.clear();
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:17:09 +01:00
|
|
|
}
|