1
0
mirror of https://github.com/clangen/musikcube.git synced 2025-03-15 07:21:23 +00:00

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
src
core/i18n
musikbox
app/layout
data/locales
stdafx.h

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

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

@ -47,8 +47,6 @@
#define MESSAGE_INDEXER_PROGRESS 1001 #define MESSAGE_INDEXER_PROGRESS 1001
#define MESSAGE_INDEXER_FINISHED 1002 #define MESSAGE_INDEXER_FINISHED 1002
#define SYNCING_TEXT_FORMAT "syncing metadata (%d tracks processed)"
using namespace musik; using namespace musik;
using namespace musik::box; using namespace musik::box;
using namespace musik::core; using namespace musik::core;
@ -56,8 +54,13 @@ using namespace musik::core::runtime;
using namespace cursespp; using namespace cursespp;
static void updateSyncingText(TextLabel* label, int updates) { static void updateSyncingText(TextLabel* label, int updates) {
label->SetText(boost::str(boost::format( try {
SYNCING_TEXT_FORMAT) % updates), cursespp::text::AlignCenter); 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) MainLayout::MainLayout(ILibraryPtr library)

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

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