Skip to content

MPDX-9947 - Enforce useTranslation and disable use of i18n singleton - #1988

Open
zweatshirt wants to merge 2 commits into
mainfrom
enforce-usetranslation
Open

MPDX-9947 - Enforce useTranslation and disable use of i18n singleton#1988
zweatshirt wants to merge 2 commits into
mainfrom
enforce-usetranslation

Conversation

@zweatshirt

@zweatshirt zweatshirt commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Description

Jira ticket: https://jira.cru.org/browse/MPDX-9947


Issue:

i18n.t() at module scope evaluates to a string at import time, before React renders and before translations have loaded. Since module bodies don't re-execute, those strings are frozen in English for the life of the page.

Two things in our setup make this unavoidable:

  • src/lib/i18n.ts uses i18next-http-backend, so translation.json is fetched over the network. .init() is called synchronously but not awaited.
  • UserPreferenceProvider calls i18n.changeLanguage() in a useEffect, after the GetUser query resolves

Fix: Why useTranslation instead of i18n.t

useTranslation() does two things, and the fix needs both:

  1. t is called inside the component body, so the expression re-evaluates on every render instead of once.
  2. The hook subscribes the component to languageChanged (react-i18next's bindI18n default) and
    calls setT with a fresh function, which guarantees that re-render actually happens when
    UserPreferenceProvider applies the saved language.

Important files to review in this PR

  • .eslintrc.js
  • src/components/User/Preferences/UserPreferenceProvider.tsx
  • src/components/Layouts/Primary/TopBar/Items/AddMenu/Items/AddDonation/AddDonation.tsx
  • src/components/HrTools/AdditionalSalaryRequest/Shared/Helper/getHeader.ts
  • src/components/Reports/Shared/SettingsDialog/SettingsDialog.tsx
  • src/components/Tool/Appeal/InitialPage/AddAppealForm/AddAppealForm.tsx
  • src/components/Tool/Home/ToolsListHome.ts
  • src/components/Contacts/ContactDetails/ContactDetailsTab/Other/ContactDetailsOther.tsx
  • tests/eslintrules/restrictedSyntaxHarness.ts
  • src/components/Tool/FixCommitmentInfo/InputOptions/Frequencies.ts

Testing

Go to Settings → Preferences → Language → Deutsch, save, then open /accountLists/id/tools.
- On old code the tool names and descriptions stay English while the surrounding page chrome translates.
- In the preview environment with new code, the tool names and descriptions will change to the correct language.

Checklist:

  • I have given my PR a title with the format "MPDX-(JIRA#) (summary sentence max 80 chars)"
  • I have applied the appropriate labels (Add the label "Preview" to automatically create a preview environment)
  • I have run the Claude Code /quality:agent-review command locally and fixed any relevant suggestions
  • I have requested a review from another person on the project
  • I have tested my changes in preview or in staging
  • I have cleaned up my commit history

@zweatshirt zweatshirt self-assigned this Aug 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Bundle sizes [mpdx-react]

Compared against 5f0edc3

No significant changes found

@zweatshirt zweatshirt added the Preview Environment Add this label to create an Amplify Preview label Aug 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Preview branch generated at https://enforce-usetranslation.d3dytjb8adxkk5.amplifyapp.com

@zweatshirt zweatshirt changed the title Enforce useTranslation and disable use of i18n singleton MPDX-9947 - Enforce useTranslation and disable use of i18n singleton Aug 14, 2026
@zweatshirt
zweatshirt marked this pull request as ready for review August 14, 2026 20:01
@zweatshirt
zweatshirt requested a review from dr-bizz August 14, 2026 20:15
@zweatshirt

Copy link
Copy Markdown
Contributor Author

@dr-bizz Thanks for considering this. The core code is a lot less than the change implies. I listed the files that are important to look at.

I did an agent review locally, it is mostly happy with the results, but this deserves a human look. Thanks Daniel!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Preview Environment Add this label to create an Amplify Preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant