How to Integrate i18next with Weaverse Hydrogen
This guide explains how to integrate i18next as the primary translation engine in your Weaverse Hydrogen project. While Weaverse provides a built-inuseThemeText() hook for simple { {variable} } substitution, integrating i18next enables advanced localization features like plurals, context-based formatting, and ordinals—without breaking Weaverse’s visual translation workflow!
How It Works
Weaverse’s resolution chain allows you to inject anexternalT function into the withWeaverse configuration. When provided, Weaverse evaluates translations in the following priority:
- External t (i18next) ← Highest priority
- Merchant Overrides (from Weaverse Studio)
- Static Content (
schema.i18n.staticContent) - Translation Key ← Fallback
i18next first and gracefully falling back to Weaverse if strings aren’t matched, your components enjoy full i18next capabilities while giving non-technical merchants complete control inside the Weaverse builder.
Step 1: Install Dependencies
You only need the corei18next package. React bindings (react-i18next) are fully optional unless you strictly prefer using them in your components over useThemeText.
Step 2: Create the i18n Bridge
Create an initialization and bridge file (e.g.,app/utils/i18n.ts) that holds your i18next singleton and the adapter function (weaverseT) expected by Weaverse.
Step 3: Configure Weaverse at the Root level
You now need to injectweaverseT into Weaverse and guarantee initI18n runs before your app renders the React tree. Open your app/root.tsx (or equivalent layout file).
Add the externalT injection:
Pass the weaverseT instance to your withWeaverse wrapper configuration.
Initialize the language on the Server:
Inside your routeloader, extract the storefront locale and explicitly trigger initI18n using your base JSON bundle.
Sync Client Hydration:
If the user transitions languages seamlessly via client-side routing, add auseEffect within the RootLayout implementation to sync i18next on the browser.
Step 4: Write Components
Your components don’t need any updates. Standard keys simply fall back to Weaverse, but keys mapped in youri18next bundles using advanced interpolations now resolve correctly!
Theme JSON Example:
Summary Checklist
- Install
i18next. - Create an i18n bridge script containing a secure async init method and the
weaverseTproxy. - Pass the localized locale variable to
initI18ninroot.tsxloader. - Wrap
RootLayoutviawithWeaverse(RootLayout, { t: weaverseT }). - Enjoy full i18next pluralization combined with Weaverse’s visual translation integration!