Skip to content

Persist user-selected locale and add a locale switcher backed by I18nProviderΒ #313

Description

@Jagadeeshftw

πŸ“Œ Description

src/shared/i18n/I18nProvider.tsx and useTranslation provide localized strings, but there is no mechanism to choose or persist a locale β€” the provider mounts a fixed locale above the router in App.tsx. Add locale state (persisted to localStorage and synced to <html lang>) exposed via the i18n context, plus a minimal programmatic API (setLocale) so a future switcher and SSR/lang-attribute correctness work. This is the data/state-management side of i18n (the UI control belongs to the other lane).

πŸ’‘ Why it matters: A fixed locale with no persistence blocks real multi-language support and leaves <html lang> wrong for screen readers.

🧩 Requirements and context

  • Add locale + setLocale to the i18n context with a typed locale union.
  • Persist the selection to localStorage and restore on mount.
  • Sync document.documentElement.lang whenever locale changes.
  • Fall back to a default locale when the stored value is unknown.
  • Keep messages.ts keyed by locale; default English remains the fallback.

Non-functional requirements

  • Must be secure, tested, and documented.
  • Should be efficient and easy to review.

πŸ› οΈ Suggested execution

1. Fork the repo and create a branch

git checkout -b feat/locale-persistence

2. Implement changes

  • Write/modify the relevant source: src/shared/i18n/I18nProvider.tsx, messages.ts, index.ts
  • Write comprehensive tests: src/shared/i18n/i18n.test.tsx
  • Add documentation: TSDoc + README i18n note
  • Include TSDoc doc comments
  • Validate security assumptions: stored locale validated against the allowed union

3. Test and commit

  • Run tests:
npm test -- i18n
  • Cover edge cases: unknown stored locale, missing storage, lang attribute sync
  • Include test output and security notes in the PR description.

Example commit message

feat(i18n): persist locale and expose setLocale via provider

βœ… Acceptance criteria

  • locale/setLocale on the i18n context
  • Selection persisted and restored
  • <html lang> synced on change
  • Unknown stored locale falls back safely

πŸ”’ Security notes

Validate the persisted locale against the allowed union to avoid using attacker-set storage values as message keys.

πŸ“‹ Guidelines

  • Minimum 95% test coverage
  • Clear documentation
  • Timeframe: 96 hours

Metadata

Metadata

Assignees

Labels

GrantFox OSSGrantFox open-source programMaybe RewardedGrantFox: potentially rewarded contributionOfficial CampaignGrantFox official campaign issuearchitectureArchitecture / structural changeenhancementNew feature or requestfrontendFrontend / UI work
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions