From 70f301941b140cf77533f361f1b09e98069048be Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Fri, 25 Oct 2024 22:38:55 +0200 Subject: [PATCH] Feature: Implement language pack support for display texts --- include/I18n.h | 9 +++- src/Display_Graphic.cpp | 21 ++++++-- src/I18n.cpp | 73 +++++++++++++++++++++++++++- webapp/src/views/DeviceAdminView.vue | 27 +++++++--- 4 files changed, 114 insertions(+), 16 deletions(-) diff --git a/include/I18n.h b/include/I18n.h index 6d75ea3..7187184 100644 --- a/include/I18n.h +++ b/include/I18n.h @@ -16,7 +16,14 @@ public: I18nClass(); void init(Scheduler& scheduler); std::list getAvailableLanguages(); - String getFilenameByLocale(String& locale) const; + String getFilenameByLocale(const String& locale) const; + void readDisplayStrings( + const String& locale, + String& date_format, + String& offline, + String& power_w, String& power_kw, + String& yield_today_wh, String& yield_today_kwh, + String& yield_total_kwh, String& yield_total_mwh); private: void readLangPacks(); diff --git a/src/Display_Graphic.cpp b/src/Display_Graphic.cpp index 69b298e..794dbdb 100644 --- a/src/Display_Graphic.cpp +++ b/src/Display_Graphic.cpp @@ -4,6 +4,7 @@ */ #include "Display_Graphic.h" #include "Datastore.h" +#include "I18n.h" #include #include #include @@ -169,14 +170,24 @@ void DisplayGraphicClass::setLocale(const String& locale) idx = I18N_LOCALE_FR; } - _i18n_offline = i18n_offline[idx]; - _i18n_yield_today_kwh = i18n_yield_today_kwh[idx]; - _i18n_yield_today_wh = i18n_yield_today_wh[idx]; _i18n_date_format = i18n_date_format[idx]; - _i18n_current_power_kw = i18n_current_power_kw[idx]; + _i18n_offline = i18n_offline[idx]; _i18n_current_power_w = i18n_current_power_w[idx]; - _i18n_yield_total_mwh = i18n_yield_total_mwh[idx]; + _i18n_current_power_kw = i18n_current_power_kw[idx]; + _i18n_yield_today_wh = i18n_yield_today_wh[idx]; + _i18n_yield_today_kwh = i18n_yield_today_kwh[idx]; _i18n_yield_total_kwh = i18n_yield_total_kwh[idx]; + _i18n_yield_total_mwh = i18n_yield_total_mwh[idx]; + + I18n.readDisplayStrings(locale, + _i18n_date_format, + _i18n_offline, + _i18n_current_power_w, + _i18n_current_power_kw, + _i18n_yield_today_wh, + _i18n_yield_today_kwh, + _i18n_yield_total_kwh, + _i18n_yield_total_mwh); } void DisplayGraphicClass::setDiagramMode(DiagramMode_t mode) diff --git a/src/I18n.cpp b/src/I18n.cpp index 0827ed1..60bbf42 100644 --- a/src/I18n.cpp +++ b/src/I18n.cpp @@ -25,7 +25,7 @@ std::list I18nClass::getAvailableLanguages() return _availLanguages; } -String I18nClass::getFilenameByLocale(String& locale) const +String I18nClass::getFilenameByLocale(const String& locale) const { auto it = std::find_if(_availLanguages.begin(), _availLanguages.end(), [locale](const LanguageInfo_t& elem) { return elem.code == locale; @@ -38,6 +38,75 @@ String I18nClass::getFilenameByLocale(String& locale) const } } +void I18nClass::readDisplayStrings( + const String& locale, + String& date_format, + String& offline, + String& power_w, String& power_kw, + String& yield_today_wh, String& yield_today_kwh, + String& yield_total_kwh, String& yield_total_mwh) +{ + auto filename = getFilenameByLocale(locale); + if (filename == "") { + return; + } + + JsonDocument filter; + filter["display"] = true; + + File f = LittleFS.open(filename, "r", false); + + JsonDocument doc; + + // Deserialize the JSON document + const DeserializationError error = deserializeJson(doc, f, DeserializationOption::Filter(filter)); + if (error) { + MessageOutput.printf("Failed to read file %s\r\n", filename.c_str()); + f.close(); + return; + } + + if (!Utils::checkJsonAlloc(doc, __FUNCTION__, __LINE__)) { + return; + } + + auto displayData = doc["display"]; + + if (displayData["date_format"].as() != "null") { + date_format = displayData["date_format"].as(); + } + + if (displayData["offline"].as() != "null") { + offline = displayData["offline"].as(); + } + + if (displayData["power_w"].as() != "null") { + power_w = displayData["power_w"].as(); + } + + if (displayData["power_kw"].as() != "null") { + power_kw = displayData["power_kw"].as(); + } + + if (displayData["yield_today_wh"].as() != "null") { + yield_today_wh = displayData["yield_today_wh"].as(); + } + + if (displayData["yield_today_kwh"].as() != "null") { + yield_today_kwh = displayData["yield_today_kwh"].as(); + } + + if (displayData["yield_total_kwh"].as() != "null") { + yield_total_kwh = displayData["yield_total_kwh"].as(); + } + + if (displayData["yield_total_mwh"].as() != "null") { + yield_total_mwh = displayData["yield_total_mwh"].as(); + } + + f.close(); +} + void I18nClass::readLangPacks() { auto root = LittleFS.open("/"); @@ -79,7 +148,7 @@ void I18nClass::readConfig(String file) lang.name = String(doc["meta"]["name"] | ""); lang.filename = file; - if (lang.code != "" && lang.name != "") { + if (lang.code != "" && lang.name != "") { _availLanguages.push_back(lang); } else { MessageOutput.printf("Invalid meta data\r\n"); diff --git a/webapp/src/views/DeviceAdminView.vue b/webapp/src/views/DeviceAdminView.vue index 8a554f7..3901de7 100644 --- a/webapp/src/views/DeviceAdminView.vue +++ b/webapp/src/views/DeviceAdminView.vue @@ -1,5 +1,5 @@