[2.x] fix(translator): preserve translations when switching locale#4400
Merged
[2.x] fix(translator): preserve translations when switching locale#4400
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
format-message#4088 (switch toformat-message) where non-English language packs had their translations silently discardedsetLocale()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 beforeapp.load()callssetLocale(), their translations were stored underformat-message's default locale ('en'). WhensetLocale('pl')ran, it set translations to{ pl: {} }, discarding everythingsetLocale()is lostaddTranslations()beforesetLocale())Test plan
flarum-lang/polish) — translations should now load correctlyyarn jest tests/unit/common/utils/Translator.test.tspasses (6 tests)🤖 Generated with Claude Code