i18next-react
v0.2.2
Published
A tiny React binding for i18next
Downloads
11,529
Readme
i18next-react
A tiny (~130LOC) React binding for i18next.
Features
Simple
- actually, the only dependencies are
react
andi18next
- actually, the only dependencies are
Framework / configuration agnostic
- opinionless about other parts of system, including how to load i18n resources / how to infer and persist locales
- easier to integrate into an existing app, or opt out
Comparison with alternatives
This library is initially made when trying to use i18next
in a Next.js serverless app.
Before rolling yet another binding, I tried these libraries:
- isaachinman/next-i18next has impressively rich features. But it requires some certain app structure, a custom server, and a few middlewares, all of which I had difficulties to comply with.
- i18next/react-i18next was not very simple to use in Next.js SSR.
Getting started
- Install:
npm install --save i18next-react
oryarn add i18next-react
- Define a factory function to provide initialized
i18next
instance - Wrap top level component (or, a component where you want to use i18next within) with
<I18NextReactProvider lang={initialLang} factory={i18NextFactory}>
- Consume
i18next
API withuseI18n()
oruseI18nLoaded(lang, namespace)
hooks
Examples
- based on CRA react app
- bundles in en locale
- load zhHans / zhHant / ja locales on demand, with i18next/i18next-http-backend
- TODO
Feedback / Issues / Contributions
If you find anything interesting, feel free to fill an issue.
Also, I haven't tested but this should be compatible with preact / react-native too. Like to hear if this library works (or doesn't) with them.
License
MIT