fix: Use useTranslation() hook server-side for proper i18n SSR#639
fix: Use useTranslation() hook server-side for proper i18n SSR#639baptistegrimaud wants to merge 1 commit intomainfrom
Conversation
2a3fdcc to
0d083e3
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses i18n conflicts when rendering multiple JavaScript modules on the same page by ensuring translations resolve against the correct module namespace during SSR and hydration (Issue #317).
Changes:
- Replace direct
tusage from thei18nextsingleton withuseTranslation()in server-rendered React components (samples and template). - Set
defaultNSonI18nextProviderduring SSR (view renderer + islands) souseTranslation()defaults to the module bundle key namespace. - Extend the Cypress i18n E2E test to validate SSR + hydration behavior with components from two different modules on the same page.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/cypress/e2e/ui/testI18n.cy.ts | Expands E2E coverage to assert SSR + hydrated translations for multiple modules on one page. |
| samples/hydrogen/src/components/LanguageSwitcher/default.server.tsx | Switches server component translation usage to useTranslation() for context-aware SSR. |
| samples/hydrogen/src/components/HelloWorld/default.server.tsx | Switches server component translation usage to useTranslation() for context-aware SSR. |
| javascript-modules-library/src/components/render/Island.tsx | Provides defaultNS={bundleKey} to scope translations correctly within islands during SSR. |
| javascript-modules-engine/src/server/init-react.tsx | Provides defaultNS={bundleKey} during SSR view rendering (and stops mutating global default namespace). |
| javascript-create-module/templates/hello-world/src/components/Hello/World/default.server.tsx | Updates template server component to use useTranslation() to match the new SSR i18n approach. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| cy.get( | ||
| 'p:contains("Welcome to Jahia! You successfully created a new JavaScript Module and a Jahia Website built with it. Here are a few things you can do now:")', | ||
| ).should("have.length", 2); |
There was a problem hiding this comment.
The SSR assertion for the Hydrogen component is coupled to the full English translation string via a p:contains("Welcome to Jahia! ...") selector. This will be brittle if the sample copy changes (even punctuation), causing unrelated i18n/SSR changes to fail the test. Consider selecting via a stable attribute (e.g., add a data-testid on the HelloWorld "welcome" paragraph) or assert on a smaller, intentionally stable substring.
| cy.get( | |
| 'p:contains("Welcome to Jahia! You successfully created a new JavaScript Module and a Jahia Website built with it. Here are a few things you can do now:")', | |
| ).should("have.length", 2); | |
| cy.get('p:contains("Welcome to Jahia!")').should("have.length", 2); |
There was a problem hiding this comment.
well, we're testing translations 🤷
Fixes #317.
Description
Use
useTranslation()hook in server components: Replaced directimport { t } from "i18next"with usage ofuseTranslation()hook in all React server components (HelloWorld,LanguageSwitcher, etc.). This ensures translations are context-aware and avoid hydration mismatches between server and client.Also, explicitly set the default namespace (
defaultNS) inI18nextProviderwrapper to ensure proper translation loading for multiple modules on the same page. Its value is the bundle key (symbolic name).Testing
Enhance the existing Cypress test with components from multiple JS modules on the same page, to validate:
hydrogenmodule render server-side translationsjavascript-modules-engine-test-modulerender server-side translationsTip
Documentation to guide the reviews: How to do a code review