1
0
mirror of synced 2024-11-24 15:50:16 +01:00

lang: Don't hardcode fallback language anymore

This commit is contained in:
WerWolv 2022-01-23 22:08:19 +01:00
parent 49610f59ea
commit b3a8d02d19
3 changed files with 28 additions and 13 deletions

View File

@ -27,13 +27,18 @@ namespace hex {
operator std::string_view() const;
operator const char*() const;
[[nodiscard]] std::string_view get() const;
[[nodiscard]] const std::string& get() const;
static void loadLanguage(std::string_view language);
static void loadLanguage(const std::string &language);
static const std::map<std::string, std::string>& getSupportedLanguages();
static void setFallbackLanguage(const std::string &language);
static const std::string& getFallbackLanguage();
private:
std::string m_unlocalizedString;
static std::string s_fallbackLanguage;
};
std::string operator+(const std::string &&left, const LangEntry &&right);

View File

@ -4,6 +4,8 @@
namespace hex {
std::string LangEntry::s_fallbackLanguage;
LanguageDefinition::LanguageDefinition(std::initializer_list<std::pair<std::string, std::string>> entries) {
for (auto pair : entries)
this->m_entries.insert(pair);
@ -18,7 +20,7 @@ namespace hex {
LangEntry::LangEntry(std::string_view unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
LangEntry::operator std::string() const {
return std::string(get());
return get();
}
LangEntry::operator std::string_view() const {
@ -26,7 +28,7 @@ namespace hex {
}
LangEntry::operator const char*() const {
return get().data();
return get().c_str();
}
std::string operator+(const std::string &&left, const LangEntry &&right) {
@ -57,29 +59,28 @@ namespace hex {
return static_cast<std::string>(left) + right;
}
std::string_view LangEntry::get() const {
const std::string& LangEntry::get() const {
auto &lang = SharedData::loadedLanguageStrings;
if (lang.find(this->m_unlocalizedString) != lang.end())
if (lang.contains(this->m_unlocalizedString))
return lang[this->m_unlocalizedString];
else
return this->m_unlocalizedString;
}
void LangEntry::loadLanguage(std::string_view language) {
constexpr auto DefaultLanguage = "en-US";
void LangEntry::loadLanguage(const std::string &language) {
SharedData::loadedLanguageStrings.clear();
auto &definitions = ContentRegistry::Language::getLanguageDefinitions();
if (!definitions.contains(language.data()))
if (!definitions.contains(language))
return;
for (auto &definition : definitions[language.data()])
for (auto &definition : definitions[language])
SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
if (language != DefaultLanguage) {
for (auto &definition : definitions[DefaultLanguage])
const auto fallbackLanguage = LangEntry::getFallbackLanguage();
if (language != fallbackLanguage) {
for (auto &definition : definitions[fallbackLanguage])
SharedData::loadedLanguageStrings.insert(definition.getEntries().begin(), definition.getEntries().end());
}
}
@ -88,4 +89,12 @@ namespace hex {
return ContentRegistry::Language::getLanguages();
}
void LangEntry::setFallbackLanguage(const std::string &language) {
LangEntry::s_fallbackLanguage = language;
}
const std::string& LangEntry::getFallbackLanguage() {
return LangEntry::s_fallbackLanguage;
}
}

View File

@ -5,6 +5,7 @@ namespace hex::plugin::builtin {
void registerLanguageEnUS() {
ContentRegistry::Language::registerLanguage("English (US)", "en-US");
LangEntry::setFallbackLanguage("en-US");
ContentRegistry::Language::addLocalizations("en-US", {
/* ImHex default functionality */