2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
#include <hex/api/localization_manager.hpp>
|
2024-01-30 15:50:00 +01:00
|
|
|
#include <hex/helpers/auto_reset.hpp>
|
2023-11-21 14:38:01 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
namespace LocalizationManager {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
AutoReset<std::string> s_fallbackLanguage;
|
|
|
|
AutoReset<std::string> s_selectedLanguage;
|
|
|
|
AutoReset<std::map<std::string, std::string>> s_currStrings;
|
2023-11-21 14:38:01 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
void resetLanguageStrings() {
|
2024-01-30 11:21:34 +01:00
|
|
|
s_currStrings->clear();
|
|
|
|
s_selectedLanguage->clear();
|
2023-11-21 14:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setFallbackLanguage(const std::string &language) {
|
|
|
|
s_fallbackLanguage = language;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LanguageDefinition::LanguageDefinition(std::map<std::string, std::string> &&entries) {
|
|
|
|
for (const auto &[key, value] : entries) {
|
|
|
|
if (value.empty())
|
|
|
|
continue;
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_entries.insert({ key, value });
|
2023-11-21 14:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::map<std::string, std::string> &LanguageDefinition::getEntries() const {
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_entries;
|
2023-11-21 14:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loadLanguage(const std::string &language) {
|
|
|
|
auto &definitions = ContentRegistry::Language::impl::getLanguageDefinitions();
|
|
|
|
|
|
|
|
if (!definitions.contains(language))
|
|
|
|
return;
|
|
|
|
|
2024-02-01 10:58:45 +01:00
|
|
|
s_currStrings->clear();
|
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
for (auto &definition : definitions[language])
|
2024-01-30 11:21:34 +01:00
|
|
|
s_currStrings->insert(definition.getEntries().begin(), definition.getEntries().end());
|
2023-11-21 14:38:01 +01:00
|
|
|
|
|
|
|
const auto& fallbackLanguage = getFallbackLanguage();
|
|
|
|
if (language != fallbackLanguage) {
|
|
|
|
for (auto &definition : definitions[fallbackLanguage])
|
2024-01-30 11:21:34 +01:00
|
|
|
s_currStrings->insert(definition.getEntries().begin(), definition.getEntries().end());
|
2023-11-21 14:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
s_selectedLanguage = language;
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:57:36 +01:00
|
|
|
std::string getLocalizedString(const std::string& unlocalizedString, const std::string& language) {
|
|
|
|
if (language.empty())
|
|
|
|
return getLocalizedString(unlocalizedString, getSelectedLanguage());
|
|
|
|
|
|
|
|
auto &languageDefinitions = ContentRegistry::Language::impl::getLanguageDefinitions();
|
|
|
|
if (!languageDefinitions.contains(language))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
std::string localizedString;
|
|
|
|
for (const auto &definition : languageDefinitions[language]) {
|
|
|
|
if (definition.getEntries().contains(unlocalizedString)) {
|
|
|
|
localizedString = definition.getEntries().at(unlocalizedString);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (localizedString.empty())
|
|
|
|
return getLocalizedString(unlocalizedString, getFallbackLanguage());
|
|
|
|
|
|
|
|
return localizedString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
const std::map<std::string, std::string> &getSupportedLanguages() {
|
|
|
|
return ContentRegistry::Language::impl::getLanguages();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &getFallbackLanguage() {
|
|
|
|
return s_fallbackLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &getSelectedLanguage() {
|
|
|
|
return s_selectedLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Lang::Lang(const char *unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
2023-12-19 12:22:28 +01:00
|
|
|
Lang::Lang(const std::string &unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
|
|
|
Lang::Lang(const UnlocalizedString &unlocalizedString) : m_unlocalizedString(unlocalizedString.get()) { }
|
2023-11-21 14:38:01 +01:00
|
|
|
Lang::Lang(std::string_view unlocalizedString) : m_unlocalizedString(unlocalizedString) { }
|
|
|
|
|
2024-01-21 18:39:32 +01:00
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
Lang::operator std::string() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Lang::operator std::string_view() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Lang::operator const char *() const {
|
|
|
|
return get().c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const std::string &&left, const Lang &&right) {
|
|
|
|
return left + static_cast<std::string>(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const Lang &&left, const std::string &&right) {
|
|
|
|
return static_cast<std::string>(left) + right;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const Lang &&left, const Lang &&right) {
|
|
|
|
return static_cast<std::string>(left) + static_cast<std::string>(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const std::string_view &&left, const Lang &&right) {
|
|
|
|
return std::string(left) + static_cast<std::string>(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const Lang &&left, const std::string_view &&right) {
|
|
|
|
return static_cast<std::string>(left) + std::string(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const char *left, const Lang &&right) {
|
|
|
|
return left + static_cast<std::string>(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string operator+(const Lang &&left, const char *right) {
|
|
|
|
return static_cast<std::string>(left) + right;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string &Lang::get() const {
|
|
|
|
auto &lang = LocalizationManager::s_currStrings;
|
2024-01-30 11:21:34 +01:00
|
|
|
if (lang->contains(m_unlocalizedString))
|
|
|
|
return lang->at(m_unlocalizedString);
|
2023-11-21 14:38:01 +01:00
|
|
|
else
|
2023-12-19 13:10:25 +01:00
|
|
|
return m_unlocalizedString;
|
2023-11-21 14:38:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|