close
Skip to content

[2.x] fix(translator): preserve translations when switching locale#4400

Merged
imorland merged 1 commit into2.xfrom
im/fix-translator-setlocale-wipes-translations
Mar 6, 2026
Merged

[2.x] fix(translator): preserve translations when switching locale#4400
imorland merged 1 commit into2.xfrom
im/fix-translator-setlocale-wipes-translations

Conversation

@imorland
Copy link
Copy Markdown
Member

@imorland imorland commented Mar 6, 2026

Summary

  • Fixes a regression introduced in chore: switch formatter to format-message #4088 (switch to format-message) where non-English language packs had their translations silently discarded
  • setLocale() was replacing the entire translation store with only entries keyed to the new locale. Since locale JS files (e.g. forum-pl.js) execute synchronously before app.load() calls setLocale(), their translations were stored under format-message's default locale ('en'). When setLocale('pl') ran, it set translations to { pl: {} }, discarding everything
  • English worked by coincidence — the default locale matched
  • Fix: merge translations from the previous/default locale into the new locale key so nothing loaded before setLocale() is lost
  • Adds a test covering the real page-load order (addTranslations() before setLocale())

Test plan

  • Switch forum language to a non-English language pack (e.g. Polish flarum-lang/polish) — translations should now load correctly
  • English still works
  • yarn jest tests/unit/common/utils/Translator.test.ts passes (6 tests)

🤖 Generated with Claude Code

setLocale() was replacing the entire translation store with only entries
for the new locale. Since locale JS files (e.g. forum-pl.js) execute
before app.load() calls setLocale(), their translations were stored under
format-message's default locale ('en'). When setLocale('pl') ran, it set
translations to { pl: {} }, discarding the Polish translations entirely.

English worked by coincidence because its locale matched the default.

Fix: when switching locale, merge any translations loaded under the
previous locale into the new locale key so nothing is lost. A test
covering the pre-setLocale addTranslations() order is also added.

Regression introduced in #4088.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@imorland imorland requested a review from a team as a code owner March 6, 2026 08:07
@imorland imorland added this to the 2.0.0-beta.8 milestone Mar 6, 2026
@imorland imorland merged commit b53f9d0 into 2.x Mar 6, 2026
34 checks passed
@imorland imorland deleted the im/fix-translator-setlocale-wipes-translations branch March 6, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant