Que fait i18next ?
Aperçu
i18next est le framework d'internationalisation (i18n) JavaScript le plus populaire. Fonctionne avec React, Vue, Angular, Node.js. Fonctionnalités : découpage par espace de noms, chargement différé, pluriels, interpolation, contexte et plugins backend pour traductions distantes.
Instructions
Étape 1 : Configuration React
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import HttpBackend from 'i18next-http-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
i18n.use(HttpBackend).use(LanguageDetector).use(initReactI18next).init({
fallbackLng: 'en',
supportedLngs: ['en', 'de', 'fr', 'ja'],
ns: ['common', 'dashboard'],
defaultNS: 'common',
backend: { loadPath: '/locales/{{lng}}/{{ns}}.json' },
})
Étape 2 : Utilisation
import { useTranslation, Trans } from 'react-i18next'
function Dashboard() {
const { t } = useTranslation('dashboard')
return (
<div>
<h1>{t('welcome', { name: 'Alice' })}</h1>
<p>{t('projects', { count: 5 })}</p>
<Trans i18nKey="terms">Accepter les <a href="/terms">Conditions</a>.</Trans>
</div>
)
}
{
"welcome": "Bon retour, {{name}} !",
"projects_one": "Vous avez {{count}} projet",
"projects_other": "Vous avez {{count}} projets"
}
Bonnes pratiques
- Découpez les traductions en espaces de noms par fonctionnalité — ne chargez pas tout en une fois.
- Utilisez les suffixes
_one/_otherpour les pluriels. - Utilisez
<Trans>pour les traductions contenant du JSX intégré. - Détection de la langue : navigateur → URL → cookie → localStorage.
- Pour SSR (rendu côté serveur) : transmettez la langue depuis le serveur pour éviter les incohérences d'hydratation.