fix: prevent i18n singleton and React locale state desync (#1160)#1234
Open
wendyamoni-creator wants to merge 1 commit into
Conversation
i18n.setLocale() was a no-op for unimplemented locales (es/fr/de) but
returned void, so useI18n's wrapper always called setLocaleState()
regardless of whether the change actually applied. This left React state
showing e.g. 'es' while i18n.t() still rendered using 'en'.
Fix:
- I18n.setLocale() now returns boolean (true = applied, false = no-op)
- useI18n.setLocale() guards setLocaleState() behind that return value
Tests:
- Extend i18n.test.ts: assert return values and that internal locale
stays 'en' after an unimplemented locale request
- Add useI18n.test.ts: assert React state and singleton stay in sync
for both implemented ('en') and unimplemented (es/fr/de) locales
Closes solutions-plug#1160
|
@wendyamoni-creator Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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
Fixes #1160 — the i18n singleton's
currentLocaleand the React-visiblelocalestate fromuseI18ncould permanently disagree when a locale in theLocaletype (es,fr,de) had no corresponding entry in thetranslationsobject.Root Cause
I18n.setLocale()guarded withif (locale in translations)but returnedvoid. TheuseI18nwrapper calledsetLocaleState(newLocale)unconditionally, so React state would show (e.g.)'es'whilei18n.t()continued to resolve keys using'en'.Fix
frontend/src/lib/i18n.ts—setLocalenow returnsboolean:frontend/src/lib/hooks/useI18n.ts— state update is guarded:Tests
src/lib/__tests__/i18n.test.tswith boolean return value assertions and locale-unchanged checks fores/fr/de.src/lib/hooks/__tests__/useI18n.test.ts(new file) covering:'en'updates both React state and singleton correctly'es','fr','de'leaves React state at'en'result.current.locale === i18n.getLocale()always holdsChecklist
fix/the-i18n-singleton-and-react-locale-state-can-pecd frontend && npm test -- i18n— 24 tests, 2 suites, all passedcd frontend && npm run build— compiled successfully, 0 warnings