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:41] – [Configurer i18next] paolo | front:traductions [2025/09/29 17:10] (Version actuelle) – [Configurer i18next] paolo | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
# Traductions | # Traductions | ||
- | Documentation [i18next](https:// | + | Documentation [i18next](https:// |
Documentation [react-i18next](https:// | Documentation [react-i18next](https:// | ||
Documenation [Intl](https:// | Documenation [Intl](https:// | ||
Ligne 65: | Ligne 65: | ||
### Configurer i18next | ### Configurer i18next | ||
- | Pour configurer i18n, nous avons besoin d' | + | Pour cela, nous devons: |
+ | |||
+ | * Créer un fichier de configuration dans lequel | ||
``` | ``` | ||
Ligne 89: | Ligne 91: | ||
``` | ``` | ||
- | Comme nous avons " | + | |
+ | ``` | ||
+ | // 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 | ### Utiliser les traductions | ||
Ligne 96: | Ligne 146: | ||
``` | ``` | ||
+ | // 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 '' |