-
-
Notifications
You must be signed in to change notification settings - Fork 99
Try adding translations to the screen #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
35d5f80
fc7ec6f
c49eb97
8012c4f
5516d9f
63ee27c
d0cb1b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include "Translation.h" | ||
| #include "TranslationStrings.h" | ||
| #include <stdarg.h> | ||
|
|
||
| Language Translation::currentLanguage = Language::ENGLISH; | ||
|
|
||
| void Translation::setLanguage(Language lang) { | ||
| currentLanguage = lang; | ||
| } | ||
|
|
||
| Language Translation::getLanguage() { | ||
| return currentLanguage; | ||
| } | ||
|
|
||
| const char* Translation::get(TranslationKey key) { | ||
| int index = static_cast<int>(key); | ||
| if (index < 0 || index >= static_cast<int>(TranslationStrings::NUM_KEYS)) { | ||
| return "Unknown"; | ||
| } | ||
|
|
||
| const char* const* strings = nullptr; | ||
| switch (currentLanguage) { | ||
| case Language::GERMAN: | ||
| strings = TranslationStrings::GERMAN; | ||
| break; | ||
| case Language::FRENCH: | ||
| strings = TranslationStrings::FRENCH; | ||
| break; | ||
| case Language::SPANISH: | ||
| strings = TranslationStrings::SPANISH; | ||
| break; | ||
| case Language::ENGLISH: | ||
| default: | ||
| strings = TranslationStrings::ENGLISH; | ||
| break; | ||
| } | ||
|
|
||
| const char* text = strings[index]; | ||
| if (text != nullptr && text[0] != '\0') { | ||
| return text; | ||
| } | ||
|
|
||
| // Fallback to English | ||
| return TranslationStrings::ENGLISH[index]; | ||
| } | ||
|
|
||
| String Translation::format(const char* format, ...) { | ||
| char buffer[256]; | ||
| va_list args; | ||
| va_start(args, format); | ||
| vsnprintf(buffer, sizeof(buffer), format, args); | ||
| va_end(args); | ||
| return String(buffer); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| #ifndef TRANSLATION_H | ||
| #define TRANSLATION_H | ||
|
|
||
| #include <Arduino.h> | ||
|
|
||
| enum class Language { | ||
| ENGLISH, | ||
| GERMAN, | ||
| FRENCH, | ||
| SPANISH | ||
| }; | ||
|
|
||
| enum class TranslationKey { | ||
| BREW, | ||
| STEAM, | ||
| WATER, | ||
| GRIND, | ||
| SELECT_PROFILE, | ||
| STARTING, | ||
| UPDATING, | ||
| TEMPERATURE_ERROR, | ||
| AUTOTUNING, | ||
| FINISHED, | ||
| INFUSION, | ||
| BREW_PHASE, | ||
| STEPS, | ||
| PHASES, | ||
| STEP, | ||
| PHASE, | ||
| SELECTED_PROFILE, | ||
| RESTART_REQUIRED | ||
| }; | ||
|
|
||
| class Translation { | ||
| public: | ||
| static void setLanguage(Language lang); | ||
| static Language getLanguage(); | ||
| static const char* get(TranslationKey key); | ||
| static String format(const char* format, ...); | ||
|
|
||
| private: | ||
| static Language currentLanguage; | ||
| }; | ||
|
|
||
| #define TR(key) Translation::get(TranslationKey::key) | ||
|
|
||
| #endif // TRANSLATION_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| #include "TranslationStrings.h" | ||
|
|
||
| namespace TranslationStrings { | ||
| // English | ||
| const char* const ENGLISH[] = { | ||
ManfredRichthofen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "Brew", // BREW | ||
| "Steam", // STEAM | ||
| "Water", // WATER | ||
| "Grind", // GRIND | ||
| "Select profile", // SELECT_PROFILE | ||
| "Starting...", // STARTING | ||
| "Updating...", // UPDATING | ||
| "Temperature error, please restart", // TEMPERATURE_ERROR | ||
| "Autotuning...", // AUTOTUNING | ||
| "Finished", // FINISHED | ||
| "INFUSION", // INFUSION | ||
| "BREW", // BREW_PHASE | ||
| "Steps", // STEPS | ||
| "Phases", // PHASES | ||
| "step", // STEP | ||
| "phase", // PHASE | ||
| "Selected profile", // SELECTED_PROFILE | ||
| "Restart required" // RESTART_REQUIRED | ||
| }; | ||
|
|
||
| // German | ||
| const char* const GERMAN[] = { | ||
| "Kaffee", // BREW | ||
| "Dampf", // STEAM | ||
| "Wasser", // WATER | ||
| "Mahlen", // GRIND | ||
| "Profil auswählen", // SELECT_PROFILE | ||
| "Starten...", // STARTING | ||
| "Aktualisieren...", // UPDATING | ||
| "Temperaturfehler, bitte neu starten", // TEMPERATURE_ERROR | ||
| "Autotune...", // AUTOTUNING | ||
| "Fertig", // FINISHED | ||
| "INFUSION", // INFUSION | ||
| "BEZUG", // BREW_PHASE | ||
| "Schritte", // STEPS | ||
| "Phasen", // PHASES | ||
| "Schritt", // STEP | ||
| "Phase", // PHASE | ||
| "Gewähltes Profil", // SELECTED_PROFILE | ||
| "Neustart benötigt" // RESTART_REQUIRED | ||
| }; | ||
|
|
||
| // French | ||
| const char* const FRENCH[] = { | ||
| "Brew", // BREW | ||
| "Steam", // STEAM | ||
| "Water", // WATER | ||
| "Grind", // GRIND | ||
| "Select profile", // SELECT_PROFILE | ||
| "Starting...", // STARTING | ||
| "Updating...", // UPDATING | ||
| "Temperature error, please restart", // TEMPERATURE_ERROR | ||
| "Autotuning...", // AUTOTUNING | ||
| "Finished", // FINISHED | ||
| "INFUSION", // INFUSION | ||
| "BREW", // BREW_PHASE | ||
| "Steps", // STEPS | ||
| "Phases", // PHASES | ||
| "step", // STEP | ||
| "phase", // PHASE | ||
| "Selected profile", // SELECTED_PROFILE | ||
| "Restart required" // RESTART_REQUIRED | ||
| }; | ||
|
|
||
| // Spanish | ||
| const char* const SPANISH[] = { | ||
| "Elaborar", // BREW | ||
| "Vapor", // STEAM | ||
| "Agua", // WATER | ||
| "Moler", // GRIND | ||
| "Seleccionar perfil", // SELECT_PROFILE | ||
| "Iniciando...", // STARTING | ||
| "Actualizando...", // UPDATING | ||
| "Error de temperatura, favor de reiniciar", // TEMPERATURE_ERROR | ||
| "Autocalibrando...", // AUTOTUNING | ||
| "Terminado", // FINISHED | ||
| "INFUSIÓN", // INFUSION | ||
| "ELABORAR", // BREW_PHASE | ||
| "Pasos", // STEPS | ||
| "Fases", // PHASES | ||
| "paso", // STEP | ||
| "fase", // PHASE | ||
| "Perfil seleccionado", // SELECTED_PROFILE | ||
| "Reinicio requerido" // RESTART_REQUIRED | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #ifndef TRANSLATION_STRINGS_H | ||
| #define TRANSLATION_STRINGS_H | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| enum class TranslationKey; | ||
|
|
||
| namespace TranslationStrings { | ||
| extern const char* const ENGLISH[]; | ||
| extern const char* const GERMAN[]; | ||
| extern const char* const FRENCH[]; | ||
| extern const char* const SPANISH[]; | ||
ManfredRichthofen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| static constexpr size_t NUM_KEYS = 18; | ||
| } | ||
|
|
||
| #endif // TRANSLATION_STRINGS_H | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include "LanguagePlugin.h" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <display/core/Translation.h> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LanguagePlugin::LanguagePlugin(Controller *controller) : Plugin(controller) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| controller->getPluginManager()->registerPlugin(this); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void LanguagePlugin::init() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| controller->getPluginManager()->on("language:change", [this](Event const &event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int language = event.getInt("language"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setLanguage(language); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void LanguagePlugin::setLanguage(int language) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Translation::setLanguage(static_cast<Language>(language)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| controller->getSettings().setLanguage(language); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| controller->getPluginManager()->emit("ui:refresh", {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Validate and short-circuit language changes to prevent invalid state and unnecessary writes Currently, any integer is cast to Language and persisted. Add range checking and skip if unchanged. void LanguagePlugin::setLanguage(int language) {
- Translation::setLanguage(static_cast<Language>(language));
- controller->getSettings().setLanguage(language);
- controller->getPluginManager()->emit("ui:refresh", {});
+ auto newLang = static_cast<Language>(language);
+ // Clamp to known enum values
+ if (newLang != Language::ENGLISH && newLang != Language::GERMAN &&
+ newLang != Language::FRENCH && newLang != Language::SPANISH) {
+ // Ignore invalid values
+ return;
+ }
+ if (Translation::getLanguage() == newLang) {
+ return; // no-op if same language
+ }
+ Translation::setLanguage(newLang);
+ controller->getSettings().setLanguage(static_cast<int>(newLang));
+ controller->getPluginManager()->emit("ui:refresh", {});
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const char* LanguagePlugin::getName() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "Language"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const char* LanguagePlugin::getDescription() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "Language settings for the display"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bool LanguagePlugin::isEnabled() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void LanguagePlugin::setEnabled(bool enabled) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String LanguagePlugin::getConfig() const { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "{\"language\":" + String(controller->getSettings().getLanguage()) + "}"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void LanguagePlugin::setConfig(const String &config) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (config.indexOf("\"language\":") != -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int start = config.indexOf("\"language\":") + 11; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int end = config.indexOf(",", start); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (end == -1) end = config.indexOf("}", start); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (end != -1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String langStr = config.substring(start, end); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int language = langStr.toInt(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setLanguage(language); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+40
to
+51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Make config parsing tolerant to whitespace and validate bounds Parsing by fixed offset fails with spaces and will silently accept invalid values. Trim and bound-check before applying. void LanguagePlugin::setConfig(const String &config) {
- if (config.indexOf("\"language\":") != -1) {
- int start = config.indexOf("\"language\":") + 11;
- int end = config.indexOf(",", start);
- if (end == -1) end = config.indexOf("}", start);
- if (end != -1) {
- String langStr = config.substring(start, end);
- int language = langStr.toInt();
- setLanguage(language);
- }
- }
+ int keyPos = config.indexOf("\"language\"");
+ if (keyPos == -1) return;
+ int colon = config.indexOf(":", keyPos);
+ if (colon == -1) return;
+ int end = config.indexOf(",", colon);
+ if (end == -1) end = config.indexOf("}", colon);
+ if (end == -1) return;
+ String langStr = config.substring(colon + 1, end);
+ langStr.trim();
+ if (langStr.length() == 0) return;
+ int language = langStr.toInt(); // tolerates leading '+'/'-' and spaces
+ setLanguage(language);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #ifndef LANGUAGE_PLUGIN_H | ||
| #define LANGUAGE_PLUGIN_H | ||
|
|
||
| class Controller; | ||
| class PluginManager; | ||
|
|
||
| class LanguagePlugin : public Plugin { | ||
| public: | ||
| explicit LanguagePlugin(Controller *controller); | ||
|
|
||
| void setup(Controller* controller, PluginManager* pluginManager) override; | ||
| void loop() override; | ||
|
|
||
| private: | ||
| void setLanguage(int language); | ||
| }; | ||
|
|
||
| #endif // LANGUAGE_PLUGIN_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Include cstdio for vsnprintf declaration
vsnprintf is declared in /<stdio.h>. Without it, some toolchains will error.
#include <stdarg.h> +#include <cstdio>📝 Committable suggestion
🤖 Prompt for AI Agents