Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
front:traductions [2025/09/29 16:15] – [Stocker un fichier de traduction] paolo | front:traductions [2025/09/29 17:10] (Version actuelle) – [Configurer i18next] paolo | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
# Traductions | # Traductions | ||
+ | Documentation [i18next](https:// | ||
Documentation [react-i18next](https:// | Documentation [react-i18next](https:// | ||
Documenation [Intl](https:// | Documenation [Intl](https:// | ||
Ligne 6: | Ligne 7: | ||
Vous souhaitez développer une application traduites en plusieures langues. Aucun problème, **react-i18next** met à dispoistion un système qui gére des fichiers de traduction. Il permet de traduire directement du texte dans vos composants et de gérer dans des cas plus complexes des formats (date, nombre, ...). | Vous souhaitez développer une application traduites en plusieures langues. Aucun problème, **react-i18next** met à dispoistion un système qui gére des fichiers de traduction. Il permet de traduire directement du texte dans vos composants et de gérer dans des cas plus complexes des formats (date, nombre, ...). | ||
- | Nous vous invitons à consulter également l' | + | Nous vous invitons à consulter également l' |
+ | . | ||
### Stocker un fichier de traduction | ### Stocker un fichier de traduction | ||
Ligne 15: | Ligne 17: | ||
Nous ne traiterons ici la méthode " | Nous ne traiterons ici la méthode " | ||
- | |||
### Faire un fichier de traduction | ### Faire un fichier de traduction | ||
Ligne 51: | Ligne 52: | ||
" | " | ||
" | " | ||
- | " | + | " |
- | " | + | " |
}, | }, | ||
" | " | ||
Ligne 62: | Ligne 63: | ||
} | } | ||
``` | ``` | ||
+ | ### Configurer i18next | ||
+ | Pour cela, nous devons: | ||
+ | * Créer un fichier de configuration dans lequel nous avons besoin d' | ||
``` | ``` | ||
+ | // src/ | ||
+ | |||
import i18n from " | import i18n from " | ||
import { initReactI18next } from " | import { initReactI18next } from " | ||
import HttpBackend from " | import HttpBackend from " | ||
- | |||
- | // import en from " | ||
- | // import fr from " | ||
- | |||
- | // const resources = { | ||
- | // en, | ||
- | // fr, | ||
- | // }; | ||
i18n | i18n | ||
Ligne 82: | Ligne 80: | ||
.use(initReactI18next) | .use(initReactI18next) | ||
.init({ | .init({ | ||
- | // resources, | ||
- | ns: [" | ||
interpolation: | interpolation: | ||
- | escapeValue: | + | escapeValue: |
}, | }, | ||
backend: { | backend: { | ||
- | loadPath: "/ | + | loadPath: "/ |
}, | }, | ||
}); | }); | ||
- | export { i18n, resources | + | export { i18n }; |
``` | ``` | ||
+ | |||
+ | * Passer cette configuration au '' | ||
+ | |||
+ | ``` | ||
+ | // src/ | ||
+ | |||
+ | import { I18nextProvider as Provider } from " | ||
+ | import { useEffect } from " | ||
+ | import { i18n } from " | ||
+ | import { useSelector } from " | ||
+ | |||
+ | export const I18nextProvider = (props) => { | ||
+ | const { children } = props; | ||
+ | |||
+ | const settings = useSelector(state => state.settings.data) ?? {}; | ||
+ | const { lng } = settings; | ||
+ | |||
+ | useEffect(() => { | ||
+ | i18n.changeLanguage(lng); | ||
+ | }, [lng]); | ||
+ | |||
+ | return ( | ||
+ | < | ||
+ | {children} | ||
+ | </ | ||
+ | ); | ||
+ | }; | ||
+ | ``` | ||
+ | |||
+ | Comme nous sommes au sein du '' | ||
+ | |||
+ | ``` | ||
+ | // src/App.jsx | ||
+ | |||
+ | import { Router } from " | ||
+ | import { ReduxProvider } from " | ||
+ | import { I18nextProvider } from " | ||
+ | |||
+ | export const App = () => { | ||
+ | return ( | ||
+ | < | ||
+ | < | ||
+ | <Router /> | ||
+ | </ | ||
+ | </ | ||
+ | ); | ||
+ | }; | ||
+ | ``` | ||
+ | |||
+ | Désormais les traductions sont accessibles dans n' | ||
+ | . | ||
+ | ### Utiliser les traductions | ||
+ | |||
+ | Pour traduire directement dans un composant, nous pouvons utiliser le hook '' | ||
+ | |||
+ | ``` | ||
+ | // src/ | ||
+ | |||
+ | import { Input } from " | ||
+ | import { useDispatch } from " | ||
+ | import { setSession } from " | ||
+ | import { API_URL } from " | ||
+ | import { useTranslation } from " | ||
+ | |||
+ | export const Login = () => { | ||
+ | const { t } = useTranslation(); | ||
+ | |||
+ | const dispatch = useDispatch(); | ||
+ | | ||
+ | const handleFormOnSubmit = (e) => { | ||
+ | // request | ||
+ | }; | ||
+ | | ||
+ | return ( | ||
+ | <div className=" | ||
+ | <form | ||
+ | onSubmit={handleFormOnSubmit} | ||
+ | className=" | ||
+ | > | ||
+ | <Input | ||
+ | id=" | ||
+ | name=" | ||
+ | label={t(" | ||
+ | type=" | ||
+ | placeholder={t(" | ||
+ | /> | ||
+ | <Input | ||
+ | id=" | ||
+ | name=" | ||
+ | label={t(" | ||
+ | type=" | ||
+ | placeholder={t(" | ||
+ | /> | ||
+ | <button className=" | ||
+ | {t(" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | ); | ||
+ | }; | ||
+ | ``` | ||
+ | |||
+ | Pour ne pas à réécrire à chaque fois '' |