# Traductions
Documentation [i18next](https://www.i18next.com/)
Documentation [react-i18next](https://react.i18next.com/)
Documentation [Intl](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Intl)
SmartCommon intègre i18next pour gérer les traductions de votre application.
## Configuration rapide
### Fichiers de traduction
Placez vos fichiers JSON dans ''public/locales/'' :
```
public/
└── locales/
├── en.json
└── fr.json
```
### Structure d'un fichier
```
// public/locales/fr.json
{
"common": {
"save": "Enregistrer",
"cancel": "Annuler",
"delete": "Supprimer",
"loading": "Chargement..."
},
"login": {
"title": "Connexion",
"email": "Adresse email",
"password": "Mot de passe",
"submit": "Se connecter",
"error": "Identifiants incorrects"
},
"home": {
"welcome": "Bienvenue {{name}} !",
"items_count": "{{count}} élément",
"items_count_plural": "{{count}} éléments"
}
}
```
```
// public/locales/en.json
{
"common": {
"save": "Save",
"cancel": "Cancel",
"delete": "Delete",
"loading": "Loading..."
},
"login": {
"title": "Login",
"email": "Email address",
"password": "Password",
"submit": "Sign in",
"error": "Invalid credentials"
},
"home": {
"welcome": "Welcome {{name}}!",
"items_count": "{{count}} item",
"items_count_plural": "{{count}} items"
}
}
```
### Configuration du Provider
```
// appConfig.js
export const config = {
// Stocker la langue dans l'état global
globalState: {
reducers: {
settings: { lng: "fr" },
},
},
// Persister les settings
storage: {
local: ["settings"],
},
};
```
## Utilisation avec useIntl
### Hook useIntl
SmartCommon expose le hook ''useIntl'' qui simplifie l'utilisation des traductions :
```
import { useIntl } from '@cap-rel/smartcommon';
const MyComponent = () => {
const { t, lng, setLng, formatDate, formatNumber } = useIntl();
return (
{t('login.title')}
{/* Avec interpolation */}
{t('home.welcome', { name: 'Jean' })}
{/* Pluriel automatique */}
{t('home.items_count', { count: 5 })}
{/* Formatage de date */}
{formatDate(new Date())}
{/* Formatage de nombre */}
{formatNumber(1234.56)}
{/* Changer la langue */}
);
};
```
### Propriétés de useIntl
^ Propriété ^ Type ^ Description ^
| ''t'' | function | Fonction de traduction |
| ''lng'' | string | Langue actuelle |
| ''setLng'' | function | Changer la langue |
| ''formatDate'' | function | Formater une date selon la locale |
| ''formatNumber'' | function | Formater un nombre selon la locale |
| ''formatCurrency'' | function | Formater une devise |
## Exemple complet : Page de login
```
// src/components/pages/public/LoginPage/index.jsx
import { useApi, useGlobalStates, useForm, useNavigation, useIntl } from '@cap-rel/smartcommon';
import { Form, Input, Button } from '@cap-rel/smartcommon';
import { z } from 'zod';
export const LoginPage = () => {
const { t } = useIntl();
const api = useApi();
const navigate = useNavigation();
const [, setSession] = useGlobalStates('session');
const schema = z.object({
email: z.string().email(t('login.error_email')),
password: z.string().min(1, t('login.error_password')),
});
const form = useForm({ schema });
const handleSubmit = async (data) => {
const response = await api.public.post('login', { json: data });
if (response.success) {
setSession(response.data);
navigate('/');
}
};
return (
);
};
```
## Sélecteur de langue
### Composant simple
```
import { useIntl } from '@cap-rel/smartcommon';
const LanguageSelector = () => {
const { lng, setLng } = useIntl();
const languages = [
{ code: 'fr', label: 'Français', flag: '🇫🇷' },
{ code: 'en', label: 'English', flag: '🇬🇧' },
];
return (
{languages.map((lang) => (
))}
);
};
```
### Avec Select SmartCommon
```
import { useIntl } from '@cap-rel/smartcommon';
import { Select } from '@cap-rel/smartcommon';
const LanguageSelect = () => {
const { lng, setLng } = useIntl();
return (