fix: translate unit names in event panel messages#3242
fix: translate unit names in event panel messages#3242slegarraga wants to merge 2 commits intoopenfrontio:mainfrom
Conversation
|
|
WalkthroughThe change fixes unit name translation in event panel messages by mapping unit string values to localized unit_type keys before translation. When processing events_display messages, the code now copies and modifies params, converting unit names to their translated equivalents so messages display in the user's language. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/client/graphics/layers/EventsDisplay.ts`:
- Around line 387-391: In EventsDisplay.ts, avoid assigning the raw i18n key
string back into params.unit when translateText fails: capture the original unit
string before transforming (e.g., const raw = params.unit), call
translateText(unitKey), then if the returned value is the same as the key or
otherwise falsy, keep params.unit = raw (or a capitalized/raw fallback) instead
of the translation; update the block that computes unitKey/params.unit to
perform this guarded assignment so missing translations show the original unit
instead of "unit_type.xxx".
There was a problem hiding this comment.
Missing fallback: raw i18n key shown in UI when translation key is absent
translateText(unitKey) returns the key string verbatim (e.g., "unit_type.port") when the key is missing from the translation map — this is the documented behaviour in Utils.ts (if (!message) return key). Assigning that value back to params.unit without a guard means a missing or not-yet-loaded key produces output like "Your unit_type.port was captured", which is a worse regression than the original English "Port".
Add a guard that falls back to the original unit value when the key is not resolved:
🛡️ Proposed fix
if (typeof params.unit === "string") {
const unitKey =
"unit_type." + params.unit.toLowerCase().replace(/ /g, "_");
- params.unit = translateText(unitKey);
+ const translated = translateText(unitKey);
+ params.unit = translated !== unitKey ? translated : params.unit;
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/client/graphics/layers/EventsDisplay.ts` around lines 387 - 391, In
EventsDisplay.ts, avoid assigning the raw i18n key string back into params.unit
when translateText fails: capture the original unit string before transforming
(e.g., const raw = params.unit), call translateText(unitKey), then if the
returned value is the same as the key or otherwise falsy, keep params.unit = raw
(or a capitalized/raw fallback) instead of the translation; update the block
that computes unitKey/params.unit to perform this guarded assignment so missing
translations show the original unit instead of "unit_type.xxx".
|
@slegarraga What if a developer (not knowing about the need for translation) just puts "building: unit.type()"? They'll test using English and it will work. We can't be sure they'll use "unit" as a key name. And also, how can we be sure they'll send a unit translation key string, and not just untranslatable unit.type()? I think what we can do is:
|
Address review feedback: - Check all string params against unit_type translations, not just 'unit' - Keep original value if no translation exists (avoids showing raw keys)
Fixes #3071
Problem
Unit names in event panel messages show raw English type strings (e.g.,
Port) instead of translated names (e.g., DutchHaven).Solution
In
EventsDisplay.ts, before interpolating event message params, theunitparam is now translated viatranslateText("unit_type.<key>")— converting the raw unit type string (e.g.,Port) to its translation key (e.g.,unit_type.port).This affects all three event messages that include unit names:
unit_captured_by_enemycaptured_enemy_unitunit_destroyedChanges
src/client/graphics/layers/EventsDisplay.ts: Translate theunitparam before passing totranslateText()Happy to iterate on feedback!