diff options
Diffstat (limited to 'core/src/Utils/I18n.cpp')
-rw-r--r-- | core/src/Utils/I18n.cpp | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/core/src/Utils/I18n.cpp b/core/src/Utils/I18n.cpp index 645cfc5..f5a6a49 100644 --- a/core/src/Utils/I18n.cpp +++ b/core/src/Utils/I18n.cpp @@ -28,15 +28,14 @@ public: }
public:
- tsl::array_map<char, LanguageInfo> localeInfos;
- tsl::array_map<char, std::string> currentEntries;
- LanguageInfo* currentLanguage;
+ tsl::array_map<char, LanguageInfo> LocaleInfos;
+ tsl::array_map<char, std::string> CurrentEntries;
+ LanguageInfo* CurrentLanguage = nullptr;
};
-std::string findLocalizedName(const fs::path& localeFile) {
+std::string FindLocalizedName(const fs::path& localeFile) {
std::ifstream ifs{ localeFile };
if (!ifs) {
- // TODO log error
throw std::runtime_error("Failed to open locale file.");
}
@@ -45,7 +44,6 @@ std::string findLocalizedName(const fs::path& localeFile) { if (auto& name = root["$localized_name"]; name.isString()) {
return std::string(name.asCString());
} else {
- // TODO log error
throw std::runtime_error("Failed to find $localized_name in language file.");
}
}
@@ -57,8 +55,7 @@ void I18n::Init() { auto dir = fs::current_path() / "locale";
if (!fs::exists(dir)) {
- // TODO log error
- return;
+ throw std::runtime_error("Failed to find locale directory.");
}
for (auto& elm : fs::directory_iterator{ dir }) {
@@ -67,16 +64,14 @@ void I18n::Init() { auto& path = elm.path();
auto codeName = path.stem().string();
- state.localeInfos.emplace(
+ state.LocaleInfos.emplace(
codeName,
LanguageInfo{
.codeName = codeName,
- .localeName = findLocalizedName(path),
+ .localeName = FindLocalizedName(path),
.file = path,
});
}
-
- SetLanguage("en_US");
}
void I18n::Shutdown() {
@@ -92,25 +87,35 @@ void I18n::ReloadLocales() { std::string_view I18n::GetLanguage() {
auto& state = I18nState::Get();
- return state.currentLanguage->localeName;
+ return state.CurrentLanguage->localeName;
}
bool I18n::SetLanguage(std::string_view lang) {
auto& state = I18nState::Get();
- if (!state.currentLanguage) return false;
- if (state.currentLanguage->codeName == lang) return false;
+ if (state.CurrentLanguage &&
+ state.CurrentLanguage->codeName == lang)
+ {
+ return false;
+ }
- if (auto iter = state.localeInfos.find(lang); iter != state.localeInfos.end()) {
- state.currentLanguage = &iter.value();
- state.currentEntries.clear();
+ if (auto iter = state.LocaleInfos.find(lang); iter != state.LocaleInfos.end()) {
+ state.CurrentLanguage = &iter.value();
+ state.CurrentEntries.clear();
- auto& file = state.currentLanguage->file;
+ auto& file = state.CurrentLanguage->file;
std::ifstream ifs{ file };
Json::Value root;
ifs >> root;
for (auto name : root.getMemberNames()) {
+ if (name == "$localized_name") {
+ continue;
+ }
+
auto& value = root[name];
+ if (value.isString()) {
+ state.CurrentEntries.insert(name, value.asCString());
+ }
}
}
ReloadLocales();
@@ -119,8 +124,8 @@ bool I18n::SetLanguage(std::string_view lang) { std::optional<std::string_view> I18n::Lookup(std::string_view key) {
auto& state = I18nState::Get();
- auto iter = state.currentEntries.find(key);
- if (iter != state.currentEntries.end()) {
+ auto iter = state.CurrentEntries.find(key);
+ if (iter != state.CurrentEntries.end()) {
return iter.value();
} else {
return std::nullopt;
@@ -140,10 +145,12 @@ std::string_view I18n::LookupUnwrap(std::string_view key) { }
BasicTranslation::BasicTranslation(std::string_view key)
- : mContent{ I18n::LookupUnwrap(key) } {
+ // Assuming the string is null terminated, which it is here (because we store interally using std::string)
+ // TODO properly use std::string_view when imgui supports it
+ : mContent{ I18n::LookupUnwrap(key).data() } {
}
-std::string_view BasicTranslation::Get() const {
+const char* BasicTranslation::Get() const {
return mContent;
}
|