2021-02-10 18:17:09 +01:00
|
|
|
#include "hex/helpers/lang.hpp"
|
|
|
|
|
|
|
|
#include "hex/helpers/shared_data.hpp"
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:17:09 +01:00
|
|
|
LangEntry::LangEntry(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
|
|
|
|
|
|
|
LangEntry::operator std::string() const {
|
|
|
|
return std::string(get());
|
|
|
|
}
|
|
|
|
|
|
|
|
LangEntry::operator std::string_view() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
LangEntry::operator const char*() const {
|
|
|
|
return get().data();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:17:09 +01:00
|
|
|
std::string_view LangEntry::get() const {
|
2021-02-11 00:35:30 +01:00
|
|
|
auto &lang = SharedData::loadedLanguageStrings;
|
2021-02-10 18:17:09 +01:00
|
|
|
if (lang.find(this->m_unlocalizedString) != lang.end())
|
|
|
|
return lang[this->m_unlocalizedString];
|
|
|
|
else
|
|
|
|
return this->m_unlocalizedString;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LangEntry::loadLanguage(std::string_view language) {
|
2021-02-11 00:35:30 +01:00
|
|
|
constexpr auto DefaultLanguage = "en-US";
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
SharedData::loadedLanguageStrings.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
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
if (!definitions.contains(language.data()))
|
|
|
|
return;
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
for (auto &definition : definitions[language.data()])
|
|
|
|
SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
2021-02-10 18:17:09 +01:00
|
|
|
|
2021-02-11 00:35:30 +01:00
|
|
|
if (language != DefaultLanguage) {
|
|
|
|
for (auto &definition : definitions[DefaultLanguage])
|
|
|
|
SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
|
2021-02-10 18:17:09 +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
|
|
|
}
|
|
|
|
|
|
|
|
}
|