# 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
Pour les traductions, utilisez ''useTranslation'' de react-i18next :
```
import { useTranslation } from 'react-i18next';
const MyComponent = () => {
const { t, i18n } = useTranslation();
return (
{t('login.title')}
{/* Avec interpolation */}
{t('home.welcome', { name: 'Jean' })}
{/* Pluriel automatique */}
{t('home.items_count', { count: 5 })}
{/* Changer la langue */}
);
};
```
Pour le formatage de dates, SmartCommon fournit le hook ''useIntl'' :
```
import { useIntl } from '@cap-rel/smartcommon';
const { DateTimeFormat } = useIntl();
// Formate un timestamp avec Intl.DateTimeFormat
DateTimeFormat(Date.now()); // "22/02/2026, 14:30:00"
DateTimeFormat(Date.now(), 'en-US'); // "02/22/2026, 02:30:00 PM"
DateTimeFormat(Date.now(), null, { dateStyle: 'full' }); // personnalise les options
```
### Propriétés de useTranslation (react-i18next)
^ Propriété ^ Type ^ Description ^
| ''t'' | function | Fonction de traduction |
| ''i18n'' | object | Instance i18next (langue, changeLanguage, etc.) |
### Propriétés de useIntl (SmartCommon)
^ Propriété ^ Type ^ Description ^
| ''DateTimeFormat'' | function | Formater une date/heure via Intl.DateTimeFormat |
## Exemple complet : Page de login
```
// src/components/pages/public/LoginPage/index.jsx
import { useApi, useGlobalStates, useForm, useNavigation } from '@cap-rel/smartcommon';
import { Form, Input, Button } from '@cap-rel/smartcommon';
import { useTranslation } from 'react-i18next';
export const LoginPage = () => {
const { t } = useTranslation();
const api = useApi();
const nav = useNavigation();
const gst = useGlobalStates();
const form = useForm({ defaultValues: { email: '', password: '' } });
const handleSubmit = async (data) => {
try {
const user = await api.login(data);
gst.local.set('session', user);
nav.navigate('/');
} catch (error) {
console.error('Login failed:', error);
}
};
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 (