Skip to content

Commit

Permalink
Refactor i18n strings and enhance logging for translations.
Browse files Browse the repository at this point in the history
Updated i18n keys for consistency and improved translation handling. Added logging for missing and found translations in `I18n::get`. Simplified configuration translations using the `i18n` helper method for clarity and maintainability.
  • Loading branch information
flattermann committed Feb 25, 2025
1 parent 5b66d99 commit a460e45
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 32 deletions.
7 changes: 5 additions & 2 deletions firmware/src/core/utils/I18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ bool I18n::loadFile(const String &filename) {

const char *I18n::get(const String &key) {
if (s_translations.count(key)) {
return s_translations[key];
const auto val = s_translations[key];
Log.traceln("Translation found: %s -> %s", key.c_str(), val);
return val;
}
return key.c_str();
Log.warningln("Translation not found: %s", key.c_str());
return "@missingTranslation@";
}

void I18n::replacePlaceholder(String &str, int index, const String &value) {
Expand Down
34 changes: 19 additions & 15 deletions firmware/src/core/utils/MainHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ void MainHelper::setupButtons() {
}

void MainHelper::setupConfig() {
s_configManager->addConfigString("General", "timezoneLoc", &s_timezoneLocation, 30, "Timezone Location, use one from <a href='https://timezonedb.com/time-zones' target='blank'>this list</a>");
// Set language here to get i18n strings for the configuration
I18n::setLanguageId(s_configManager->getConfigInt("lang", DEFAULT_LANGUAGE));
s_configManager->addConfigString("General", "timezoneLoc", &s_timezoneLocation, 30, i18n("timezoneLoc"));
String *optLang = I18n::getAllLanguages();
s_configManager->addConfigComboBox("General", "lang", &s_languageId, optLang, LANG_NUM, "Language");
s_configManager->addConfigInt("General", "widgetCycDelay", &s_widgetCycleDelay, "Automatically cycle widgets every X seconds, set to 0 to disable");
s_configManager->addConfigString("General", "ntpServer", &s_ntpServer, 30, "NTP server", true);
String optRotation[] = {"No rotation", "Rotate 90° clockwise", "Rotate 180°", "Rotate 270° clockwise"};
s_configManager->addConfigComboBox("TFT Settings", "orbRotation", &s_orbRotation, optRotation, 4, "Orb rotation");
s_configManager->addConfigBool("TFT Settings", "nightmode", &s_nightMode, "Enable Nighttime mode");
s_configManager->addConfigInt("TFT Settings", "tftBrightness", &s_tftBrightness, "TFT Brightness [0-255]", true);
s_configManager->addConfigComboBox("General", "lang", &s_languageId, optLang, LANG_NUM, i18n("language"));
s_configManager->addConfigInt("General", "widgetCycDelay", &s_widgetCycleDelay, i18n("widgetCycleDelay"));
s_configManager->addConfigString("General", "ntpServer", &s_ntpServer, 30, i18n("ntpServer"), true);
String optRotation[] = {i18n("orbRotation.0"), i18n("orbRotation.1"), i18n("orbRotation.2"), i18n("orbRotation.3")};
s_configManager->addConfigComboBox("TFT Settings", "orbRotation", &s_orbRotation, optRotation, 4, i18n("orbRotation"));
s_configManager->addConfigBool("TFT Settings", "nightmode", &s_nightMode, i18n("nightmode"));
s_configManager->addConfigInt("TFT Settings", "tftBrightness", &s_tftBrightness, i18n("tftBrightness"), true);
String optHours[] = {"0:00", "1:00", "2:00", "3:00", "4:00", "5:00", "6:00", "7:00", "8:00", "9:00", "10:00", "11:00",
"12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"};
s_configManager->addConfigComboBox("TFT Settings", "dimStartHour", &s_dimStartHour, optHours, 24, "Nighttime Start [24h format]", true);
s_configManager->addConfigComboBox("TFT Settings", "dimEndHour", &s_dimEndHour, optHours, 24, "Nighttime End [24h format]", true);
s_configManager->addConfigInt("TFT Settings", "dimBrightness", &s_dimBrightness, "Nighttime Brightness [0-255]", true);

I18n::setLanguageId(s_languageId);
s_configManager->addConfigComboBox("TFT Settings", "dimStartHour", &s_dimStartHour, optHours, 24, i18n("dimStartHour"), true);
s_configManager->addConfigComboBox("TFT Settings", "dimEndHour", &s_dimEndHour, optHours, 24, i18n("dimEndHour"), true);
s_configManager->addConfigInt("TFT Settings", "dimBrightness", &s_dimBrightness, i18n("dimBrightness"), true);
}

void MainHelper::buttonPressed(uint8_t buttonId, ButtonState state) {
Expand Down Expand Up @@ -432,7 +432,7 @@ void MainHelper::showWelcome() {
}

s_screenManager->selectScreen(1);
s_screenManager->drawCentreString(I18n::get("infoorbs"), ScreenCenterX, ScreenCenterY - 50, 22);
s_screenManager->drawCentreString(I18n::get("infoOrbs"), ScreenCenterX, ScreenCenterY - 50, 22);
s_screenManager->drawCentreString(I18n::get("by"), ScreenCenterX, ScreenCenterY - 5, 22);
s_screenManager->drawCentreString(I18n::get("brett.tech"), ScreenCenterX, ScreenCenterY + 30, 22);
s_screenManager->setFontColor(TFT_RED);
Expand Down Expand Up @@ -507,4 +507,8 @@ void MainHelper::watchdogInit() {

void MainHelper::watchdogReset() {
esp_task_wdt_reset();
}
}

const char *MainHelper::i18n(const String &key) {
return I18n::get(key);
}
3 changes: 2 additions & 1 deletion firmware/src/core/utils/MainHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class MainHelper {
static void watchdogReset();

static void updateBrightnessByTime(uint8_t hour24);
static void setupI18n();

static const char *i18n(const String &key);
};

#endif
2 changes: 1 addition & 1 deletion firmware/src/core/widget/WidgetSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void WidgetSet::showCenteredLine(int screen, const String &text) {
}

void WidgetSet::showLoading() {
showCenteredLine(3, I18n::get("loading_data"));
showCenteredLine(3, I18n::get("loadingData"));
}

void WidgetSet::updateAll() {
Expand Down
6 changes: 3 additions & 3 deletions firmware/src/widgets/parqetwidget/ParqetWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ ParqetWidget::ParqetWidget(ScreenManager &manager, ConfigManager &config) : Widg
Serial.printf("Constructing ParqetWidget, portfolioId=%s\n", m_portfolioId.c_str());
// Load widget specific translations
I18n::loadExtraTranslations(ParqetWidget::getName());
m_config.addConfigBool("ParqetWidget", "pqEnabled", &m_enabled, i18n("enable_widget"));
m_config.addConfigString("ParqetWidget", "pqportfoId", &m_portfolioId, 50, i18n("pq.cnf.portfolio_id"));
m_config.addConfigBool("ParqetWidget", "pqEnabled", &m_enabled, i18n("enableWidget"));
m_config.addConfigString("ParqetWidget", "pqportfoId", &m_portfolioId, 50, i18n("pq.cnf.portfolioId"));
m_config.addConfigComboBox("ParqetWidget", "pqDefMode", &m_defaultMode, m_modes, PARQET_MODE_COUNT, i18n("pq.cnf.mode"), true);
m_config.addConfigComboBox("ParqetWidget", "pqDefPerf", &m_defaultPerfMeasure, m_perfMeasures, PARQET_PERF_COUNT, "Performance measure", true);
m_config.addConfigComboBox("ParqetWidget", "pqDefPerfCh", &m_defaultPerfChartMeasure, m_perfChartMeasures, PARQET_PERF_CHART_COUNT, "Chart measure", true);
m_config.addConfigBool("ParqetWidget", "pqShowClock", &m_showClock, i18n("pq.cnf.clock"), true);
m_config.addConfigBool("ParqetWidget", "pqShowTotalScr", &m_showTotalScreen, i18n("pq.cnf.totals"), true);
m_config.addConfigBool("ParqetWidget", "pqShowTotalVal", &m_showTotalValue, i18n("pq.cnf.total_val"), true);
m_config.addConfigBool("ParqetWidget", "pqShowTotalVal", &m_showTotalValue, i18n("pq.cnf.totalVal"), true);
String optPriceVal[] = {"Show current price", "Show current value"};
m_config.addConfigComboBox("ParqetWidget", "pqShowValues", &m_showValues, optPriceVal, 2, i18n("pq.cnf.values"), true);
m_config.addConfigString("ParqetWidget", "pqProxyUrl", &m_proxyUrl, 75, "ParqetProxy URL", true);
Expand Down
4 changes: 2 additions & 2 deletions i18n/de.parqet.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pq.portfolio=Portfolio
pq.total=G E S A M T
pq.cnf.portfolio_id=Portfolio-ID (muss auf öffentlich stehen!)
pq.cnf.portfolioId=Portfolio-ID (muss auf öffentlich stehen!)
pq.cnf.mode=Standardzeitraum (Zeitraum kann durch mittellanges Drücken des mittleren Buttons geändert werden)
pq.cnf.clock=Uhr auf dem ersten Bildschirm anzeigen
pq.cnf.totals=Gesamtübersicht anzeigen
pq.cnf.total_val=Gesamtwert des Portfolios anzeigen
pq.cnf.totalVal=Gesamtwert des Portfolios anzeigen
pq.cnf.values=Preis oder Wert der Aktien anzeigen
20 changes: 17 additions & 3 deletions i18n/de.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
welcome=Willkommen
infoorbs=InfoOrbs
infoOrbs=InfoOrbs
by=von
brett.tech=brett.tech
version=Version \1
loading_data=Lade Daten:
enable_widget=Widget aktivieren
loadingData=Lade Daten:
enableWidget=Widget aktivieren
timezoneLoc=Zeitzone, verwende eine aus <a href='https://timezonedb.com/time-zones' target='blank'>dieser Liste</a>
language=Sprache
widgetCycleDelay=Widgets automatisch alle X Sekunden wechseln, 0 zum Deaktivieren
ntpServer=NTP-Server
orbRotation=Orb-Drehung
orbRotation.0=Keine Drehung
orbRotation.1=90° im Uhrzeigersinn
orbRotation.2=180° drehen
orbRotation.3=270° im Uhrzeigersinn
nightmode=Nachtmodus aktivieren
tftBrightness=TFT-Helligkeit [0-255]
dimStartHour=Startzeit Nachtmodus [24h-Format]
dimEndHour=Endzeit Nachtmodus [24h-Format]
dimBrightness=Helligkeit im Nachtmodus [0-255]
4 changes: 2 additions & 2 deletions i18n/en.parqet.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pq.portfolio=Portfolio
pq.total=T O T A L
pq.cnf.portfolio_id=Portfolio ID (must be set to public!)
pq.cnf.portfolioId=Portfolio ID (must be set to public!)
pq.cnf.mode=Default timeframe (you can change timeframes by medium pressing the middle button)
pq.cnf.clock=Show clock on first screen
pq.cnf.totals=Show totals screen
pq.cnf.total_val=Show total portfolio value
pq.cnf.totalVal=Show total portfolio value
pq.cnf.values=Show price or value for stocks
20 changes: 17 additions & 3 deletions i18n/en.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
welcome=Welcome
infoorbs=InfoOrbs
infoOrbs=InfoOrbs
by=by
brett.tech=brett.tech
version=Version \1
loading_data=Loading data:
enable_widget=Enable Widget
loadingData=Loading data:
enableWidget=Enable Widget
timezoneLoc=Timezone Location, use one from <a href='https://timezonedb.com/time-zones' target='blank'>this list</a>
language=Language
widgetCycleDelay=Automatically cycle widgets every X seconds, set to 0 to disable
ntpServer=NTP server
orbRotation=Orb rotation
orbRotation.0=No rotation
orbRotation.1=Rotate 90° clockwise
orbRotation.2=Rotate 180°
orbRotation.3=Rotate 270° clockwise
nightmode=Enable Nighttime mode
tftBrightness=TFT Brightness [0-255]
dimStartHour=Nighttime Start [24h format]
dimEndHour=Nighttime End [24h format]
dimBrightness=Nighttime Brightness [0-255]

0 comments on commit a460e45

Please sign in to comment.