Skip to content

Commit

Permalink
Feature: Implement language pack support for display texts
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Oct 25, 2024
1 parent d259042 commit 70f3019
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 16 deletions.
9 changes: 8 additions & 1 deletion include/I18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ class I18nClass {
I18nClass();
void init(Scheduler& scheduler);
std::list<LanguageInfo_t> 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();
Expand Down
21 changes: 16 additions & 5 deletions src/Display_Graphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
#include "Display_Graphic.h"
#include "Datastore.h"
#include "I18n.h"
#include <NetworkSettings.h>
#include <map>
#include <time.h>
Expand Down Expand Up @@ -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)
Expand Down
73 changes: 71 additions & 2 deletions src/I18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ std::list<LanguageInfo_t> 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;
Expand All @@ -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<String>() != "null") {
date_format = displayData["date_format"].as<String>();
}

if (displayData["offline"].as<String>() != "null") {
offline = displayData["offline"].as<String>();
}

if (displayData["power_w"].as<String>() != "null") {
power_w = displayData["power_w"].as<String>();
}

if (displayData["power_kw"].as<String>() != "null") {
power_kw = displayData["power_kw"].as<String>();
}

if (displayData["yield_today_wh"].as<String>() != "null") {
yield_today_wh = displayData["yield_today_wh"].as<String>();
}

if (displayData["yield_today_kwh"].as<String>() != "null") {
yield_today_kwh = displayData["yield_today_kwh"].as<String>();
}

if (displayData["yield_total_kwh"].as<String>() != "null") {
yield_total_kwh = displayData["yield_total_kwh"].as<String>();
}

if (displayData["yield_total_mwh"].as<String>() != "null") {
yield_total_mwh = displayData["yield_total_mwh"].as<String>();
}

f.close();
}

void I18nClass::readLangPacks()
{
auto root = LittleFS.open("/");
Expand Down Expand Up @@ -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");
Expand Down
27 changes: 19 additions & 8 deletions webapp/src/views/DeviceAdminView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<BasePage :title="$t('deviceadmin.DeviceManager')" :isLoading="dataLoading || pinMappingLoading">
<BasePage :title="$t('deviceadmin.DeviceManager')" :isLoading="dataLoading || pinMappingLoading || languageLoading">
<BootstrapAlert v-model="showAlert" dismissible :variant="alertType">
{{ alertMessage }}
</BootstrapAlert>
Expand Down Expand Up @@ -162,11 +162,11 @@
<div class="col-sm-10">
<select class="form-select" v-model="deviceConfigList.display.locale">
<option
v-for="locale in displayLocaleList"
:key="locale.key"
:value="locale.key"
v-for="(locale, index) in displayLocaleList"
:key="locale.code"
:value="locale.code"
>
{{ $t(`deviceadmin.` + locale.value) }}
{{ $t((index < 3 ? `deviceadmin.` : ``) + locale.name) }}
</option>
</select>
</div>
Expand Down Expand Up @@ -277,6 +277,7 @@ export default defineComponent({
return {
dataLoading: true,
pinMappingLoading: true,
languageLoading: true,
deviceConfigList: {} as DeviceConfig,
pinMappingList: {} as PinMapping,
alertMessage: '',
Expand All @@ -290,9 +291,9 @@ export default defineComponent({
{ key: 3, value: 'rot270' },
],
displayLocaleList: [
{ key: 'en', value: 'en' },
{ key: 'de', value: 'de' },
{ key: 'fr', value: 'fr' },
{ code: 'en', name: 'en' },
{ code: 'de', name: 'de' },
{ code: 'fr', name: 'fr' },
],
diagramModeList: [
{ key: 0, value: 'off' },
Expand All @@ -304,6 +305,7 @@ export default defineComponent({
created() {
this.getDeviceConfig();
this.getPinMappingList();
this.getLanguageList();
},
watch: {
equalBrightnessCheckVal: function (val) {
Expand All @@ -314,6 +316,15 @@ export default defineComponent({
},
},
methods: {
getLanguageList() {
this.languageLoading = true;
fetch('/api/i18n/languages')
.then((response) => handleResponse(response, this.$emitter, this.$router))
.then((data) => {
this.displayLocaleList.push(...data);
this.languageLoading = false;
});
},
getPinMappingList() {
this.pinMappingLoading = true;
fetch('/api/file/get?file=pin_mapping.json', { headers: authHeader() })
Expand Down

0 comments on commit 70f3019

Please sign in to comment.