Fall back to the default locale if the string can't be found in the

selected locale. Otherwise, fallback to the key name!
This commit is contained in:
casey langen 2017-03-15 00:24:36 -07:00
parent b7a4dc344a
commit 5881a41e22
5 changed files with 47 additions and 21 deletions

View File

@ -49,6 +49,23 @@ using namespace musik::core::prefs;
using namespace boost::filesystem;
static nlohmann::json loadLocaleData(const std::string& fn) {
char* bytes = nullptr;
int count = 0;
if (FileToByteArray(fn, &bytes, count, true)) {
try {
return nlohmann::json::parse(bytes);
}
catch (...) {
}
free(bytes);
}
return nlohmann::json();
}
Locale::Locale() {
this->prefs = Preferences::ForComponent(components::Settings);
this->selectedLocale = prefs->GetString(keys::Locale, DEFAULT_LOCALE);
@ -91,7 +108,9 @@ std::string Locale::GetSelectedLocale() {
}
bool Locale::SetSelectedLocale(const std::string& locale) {
bool success = false;
if (this->defaultLocaleData.is_null()) {
this->defaultLocaleData = loadLocaleData(localePath + "/" + DEFAULT_LOCALE + ".json");
}
auto it = std::find_if(
this->locales.begin(),
@ -105,23 +124,11 @@ bool Locale::SetSelectedLocale(const std::string& locale) {
this->localeData = nlohmann::json({});
std::string localeFn = this->localePath + "/" + locale + ".json";
char* bytes = nullptr;
int count = 0;
if (FileToByteArray(localeFn, &bytes, count, true)) {
try {
this->localeData = nlohmann::json::parse(bytes);
success = true;
}
catch (...) {
}
free(bytes);
}
this->localeData = loadLocaleData(localeFn);
return !this->localeData.is_null();
}
return success;
return false;
}
std::string Locale::Translate(const std::string& key) {
@ -131,11 +138,23 @@ std::string Locale::Translate(const std::string& key) {
std::string Locale::Translate(const char* key) {
static nlohmann::json empty;
/* get the string from the current locale */
if (!this->localeData.is_null()) {
const nlohmann::json& strings = this->localeData.value(KEY_STRINGS, empty);
auto it = strings.find(key);
if (it != strings.end()) {
return it.value();
}
}
/* can't be found? fall back to the default locale */
if (!this->defaultLocaleData.is_null()) {
const nlohmann::json& strings = this->defaultLocaleData.value(KEY_STRINGS, empty);
auto it = strings.find(key);
return (it != strings.end()) ? it.value() : key;
}
/* otherwise, just return the key! */
return key;
}

View File

@ -67,6 +67,7 @@ namespace musik { namespace core { namespace i18n {
std::string selectedLocale;
std::string localePath;
nlohmann::json localeData;
nlohmann::json defaultLocaleData;
};
#define _TSTR(KEY) (musik::core::i18n::Locale::Instance().Translate(KEY))

View File

@ -47,8 +47,6 @@
#define MESSAGE_INDEXER_PROGRESS 1001
#define MESSAGE_INDEXER_FINISHED 1002
#define SYNCING_TEXT_FORMAT "syncing metadata (%d tracks processed)"
using namespace musik;
using namespace musik::box;
using namespace musik::core;
@ -56,8 +54,13 @@ using namespace musik::core::runtime;
using namespace cursespp;
static void updateSyncingText(TextLabel* label, int updates) {
label->SetText(boost::str(boost::format(
SYNCING_TEXT_FORMAT) % updates), cursespp::text::AlignCenter);
try {
label->SetText(boost::str(boost::format(
_TSTR("main_syncing_banner")) % updates), cursespp::text::AlignCenter);
}
catch (...) {
/* swallow. incomplete locale. don't crash. */
}
}
MainLayout::MainLayout(ILibraryPtr library)

View File

@ -6,6 +6,8 @@
"browse_title_genres": "genres",
"browse_title_album_artists": "album artists",
"browse_title_category": "category",
"browse_title_tracks": "tracks"
"browse_title_tracks": "tracks",
"main_syncing_banner": "syncing metadata (%d tracks processed)"
}
}

View File

@ -35,4 +35,5 @@
#pragma once
#include <core/config.h>
#include <core/i18n/Locale.h>
#include <cursespp/curses_config.h>