feat: User-Interface I18n System (#728)
## Summary **What is the goal of this PR?** This PR introduces Internationalization (i18n) support, enabling users to switch the UI language dynamically. **What changes are included?** - Core Logic: Added I18n class (`lib/I18n/I18n.h/cpp`) to manage language state and string retrieval. - Data Structures: - `lib/I18n/I18nStrings.h/cpp`: Static string arrays for each supported language. - `lib/I18n/I18nKeys.h`: Enum definitions for type-safe string access. - `lib/I18n/translations.csv`: single source of truth. - Documentation: Added `docs/i18n.md` detailing the workflow for developers and translators. - New Settings activity: `src/activities/settings/LanguageSelectActivity.h/cpp` ## Additional Context This implementation (building on concepts from #505) prioritizes performance and memory efficiency. The core approach is to store all localized strings for each language in dedicated arrays and access them via enums. This provides O(1) access with zero runtime overhead, and avoids the heap allocations, hashing, and collision handling required by `std::map` or `std::unordered_map`. The main trade-off is that enums and string arrays must remain perfectly synchronized—any mismatch would result in incorrect strings being displayed in the UI. To eliminate this risk, I added a Python script that automatically generates `I18nStrings.h/.cpp` and `I18nKeys.h` from a CSV file, which will serve as the single source of truth for all translations. The full design and workflow are documented in `docs/i18n.md`. ### Next Steps - [x] Python script `generate_i18n.py` to auto-generate C++ files from CSV - [x] Populate translations.csv with initial translations. Currently available translations: English, Español, Français, Deutsch, Čeština, Português (Brasil), Русский, Svenska. Thanks, community! **Status:** EDIT: ready to be merged. As a proof of concept, the SPANISH strings currently mirror the English ones, but are fully uppercased. --- ### AI Usage Did you use AI tools to help write this code? _**< PARTIALLY >**_ I used AI for the black work of replacing strings with I18n references across the project, and for generating the documentation. EDIT: also some help with merging changes from master. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: yeyeto2788 <juanernestobiondi@gmail.com>
This commit is contained in:
committed by
GitHub
co-authored by
google-labs-jules[bot]
yeyeto2788
parent
566a12e9b7
commit
0020e0b223
@@ -1,6 +1,7 @@
|
||||
#include "ButtonRemapActivity.h"
|
||||
|
||||
#include <GfxRenderer.h>
|
||||
#include <I18n.h>
|
||||
|
||||
#include "CrossPointSettings.h"
|
||||
#include "MappedInputManager.h"
|
||||
@@ -106,8 +107,8 @@ void ButtonRemapActivity::render(Activity::RenderLock&&) {
|
||||
return "-";
|
||||
};
|
||||
|
||||
renderer.drawCenteredText(UI_12_FONT_ID, 15, "Remap Front Buttons", true, EpdFontFamily::BOLD);
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, 40, "Press a front button for each role");
|
||||
renderer.drawCenteredText(UI_12_FONT_ID, 15, tr(STR_REMAP_FRONT_BUTTONS), true, EpdFontFamily::BOLD);
|
||||
renderer.drawCenteredText(UI_10_FONT_ID, 40, tr(STR_REMAP_PROMPT));
|
||||
|
||||
for (uint8_t i = 0; i < kRoleCount; i++) {
|
||||
const int y = 70 + i * 30;
|
||||
@@ -122,7 +123,7 @@ void ButtonRemapActivity::render(Activity::RenderLock&&) {
|
||||
renderer.drawText(UI_10_FONT_ID, 20, y, roleName, !isSelected);
|
||||
|
||||
// Show currently assigned hardware button (or unassigned).
|
||||
const char* assigned = (tempMapping[i] == kUnassigned) ? "Unassigned" : getHardwareName(tempMapping[i]);
|
||||
const char* assigned = (tempMapping[i] == kUnassigned) ? tr(STR_UNASSIGNED) : getHardwareName(tempMapping[i]);
|
||||
const auto width = renderer.getTextWidth(UI_10_FONT_ID, assigned);
|
||||
renderer.drawText(UI_10_FONT_ID, pageWidth - 20 - width, y, assigned, !isSelected);
|
||||
}
|
||||
@@ -133,8 +134,8 @@ void ButtonRemapActivity::render(Activity::RenderLock&&) {
|
||||
}
|
||||
|
||||
// Provide side button actions at the bottom of the screen (split across two lines).
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, 250, "Side button Up: Reset to default layout", true);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, 280, "Side button Down: Cancel remapping", true);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, 250, tr(STR_REMAP_RESET_HINT), true);
|
||||
renderer.drawCenteredText(SMALL_FONT_ID, 280, tr(STR_REMAP_CANCEL_HINT), true);
|
||||
|
||||
// Live preview of logical labels under front buttons.
|
||||
// This mirrors the on-device front button order: Back, Confirm, Left, Right.
|
||||
@@ -157,7 +158,7 @@ bool ButtonRemapActivity::validateUnassigned(const uint8_t pressedButton) {
|
||||
// Block reusing a hardware button already assigned to another role.
|
||||
for (uint8_t i = 0; i < kRoleCount; i++) {
|
||||
if (tempMapping[i] == pressedButton && i != currentStep) {
|
||||
errorMessage = "Already assigned";
|
||||
errorMessage = tr(STR_ALREADY_ASSIGNED);
|
||||
errorUntil = millis() + kErrorDisplayMs;
|
||||
return false;
|
||||
}
|
||||
@@ -168,27 +169,27 @@ bool ButtonRemapActivity::validateUnassigned(const uint8_t pressedButton) {
|
||||
const char* ButtonRemapActivity::getRoleName(const uint8_t roleIndex) const {
|
||||
switch (roleIndex) {
|
||||
case 0:
|
||||
return "Back";
|
||||
return tr(STR_BACK);
|
||||
case 1:
|
||||
return "Confirm";
|
||||
return tr(STR_CONFIRM);
|
||||
case 2:
|
||||
return "Left";
|
||||
return tr(STR_DIR_LEFT);
|
||||
case 3:
|
||||
default:
|
||||
return "Right";
|
||||
return tr(STR_DIR_RIGHT);
|
||||
}
|
||||
}
|
||||
|
||||
const char* ButtonRemapActivity::getHardwareName(const uint8_t buttonIndex) const {
|
||||
switch (buttonIndex) {
|
||||
case CrossPointSettings::FRONT_HW_BACK:
|
||||
return "Back (1st button)";
|
||||
return tr(STR_HW_BACK_LABEL);
|
||||
case CrossPointSettings::FRONT_HW_CONFIRM:
|
||||
return "Confirm (2nd button)";
|
||||
return tr(STR_HW_CONFIRM_LABEL);
|
||||
case CrossPointSettings::FRONT_HW_LEFT:
|
||||
return "Left (3rd button)";
|
||||
return tr(STR_HW_LEFT_LABEL);
|
||||
case CrossPointSettings::FRONT_HW_RIGHT:
|
||||
return "Right (4th button)";
|
||||
return tr(STR_HW_RIGHT_LABEL);
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user