SmarMaker - Documentation
Docs» 03_front:traductions

Traductions

Documentation i18next Documentation react-i18next Documentation 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 (
    <div>
      <h1>{t('login.title')}</h1>

      {/* Avec interpolation */}
      <p>{t('home.welcome', { name: 'Jean' })}</p>

      {/* Pluriel automatique */}
      <p>{t('home.items_count', { count: 5 })}</p>

      {/* Formatage de date */}
      <p>{formatDate(new Date())}</p>

      {/* Formatage de nombre */}
      <p>{formatNumber(1234.56)}</p>

      {/* Changer la langue */}
      <button onClick={() => setLng('en')}>English</button>
      <button onClick={() => setLng('fr')}>Français</button>
    </div>
  );
};

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 (
    <div className="fixed inset-0 bg-white flex justify-center items-center p-10">
      <Form form={form} onSubmit={handleSubmit} className="flex flex-col gap-6 w-full max-w-sm">
        <h1 className="text-2xl font-bold text-center">
          {t('login.title')}
        </h1>

        <Input
          name="email"
          type="email"
          label={t('login.email')}
          placeholder={t('login.email_placeholder')}
        />

        <Input
          name="password"
          type="password"
          label={t('login.password')}
          placeholder="●●●●●●●●"
        />

        <Button type="submit" loading={form.formState.isSubmitting}>
          {t('login.submit')}
        </Button>
      </Form>
    </div>
  );
};

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 (
    <div className="flex gap-2">
      {languages.map((lang) => (
        <button
          key={lang.code}
          onClick={() => setLng(lang.code)}
          className={`px-3 py-1 rounded ${
            lng === lang.code
              ? 'bg-primary text-white'
              : 'bg-gray-200'
          }`}
        >
          {lang.flag} {lang.label}
        </button>
      ))}
    </div>
  );
};

Avec Select SmartCommon

import { useIntl } from '@cap-rel/smartcommon';
import { Select } from '@cap-rel/smartcommon';

const LanguageSelect = () => {
  const { lng, setLng } = useIntl();

  return (
    <Select
      value={lng}
      onChange={(e) => setLng(e.target.value)}
      options={[
        { value: 'fr', label: 'Français' },
        { value: 'en', label: 'English' },
        { value: 'es', label: 'Español' },
      ]}
    />
  );
};

Formatage avancé

Dates

import { useIntl } from '@cap-rel/smartcommon';

const DateDisplay = ({ date }) => {
  const { formatDate } = useIntl();

  return (
    <div>
      {/* Format par défaut */}
      <p>{formatDate(date)}</p>

      {/* Format personnalisé */}
      <p>{formatDate(date, { dateStyle: 'full' })}</p>
      <p>{formatDate(date, { dateStyle: 'long', timeStyle: 'short' })}</p>
    </div>
  );
};

Nombres et devises

import { useIntl } from '@cap-rel/smartcommon';

const PriceDisplay = ({ amount }) => {
  const { formatNumber, formatCurrency } = useIntl();

  return (
    <div>
      {/* Nombre simple */}
      <p>{formatNumber(1234567.89)}</p>
      {/* fr: 1 234 567,89 */}
      {/* en: 1,234,567.89 */}

      {/* Devise */}
      <p>{formatCurrency(amount, 'EUR')}</p>
      {/* fr: 1 234,56 € */}
      {/* en: €1,234.56 */}
    </div>
  );
};

Interpolation et pluriels

Interpolation

// fr.json
{
  "greeting": "Bonjour {{name}}, vous avez {{count}} messages"
}

// Utilisation
t('greeting', { name: 'Jean', count: 5 })
// "Bonjour Jean, vous avez 5 messages"

Pluriels

// fr.json
{
  "item": "{{count}} article",
  "item_plural": "{{count}} articles",
  "item_zero": "Aucun article"
}

// Utilisation
t('item', { count: 0 })  // "Aucun article"
t('item', { count: 1 })  // "1 article"
t('item', { count: 5 })  // "5 articles"

Organisation avancée

Namespaces

Pour les grandes applications, organisez par namespace :

public/
└── locales/
    ├── fr/
    │   ├── common.json
    │   ├── login.json
    │   └── dashboard.json
    └── en/
        ├── common.json
        ├── login.json
        └── dashboard.json
// Utilisation avec namespace
t('login:title')
t('dashboard:stats.revenue')

Prefix automatique

Pour éviter de répéter le namespace :

import { useTranslation } from 'react-i18next';

const LoginPage = () => {
  // Utiliser un préfixe
  const { t } = useTranslation('login');

  return (
    <div>
      <h1>{t('title')}</h1>           {/* login.title */}
      <p>{t('description')}</p>        {/* login.description */}
    </div>
  );
};

Configuration manuelle (sans SmartCommon)

Si vous n'utilisez pas SmartCommon, voici la configuration manuelle :

// src/i18n/index.js

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import HttpBackend from "i18next-http-backend";

i18n
  .use(HttpBackend)
  .use(initReactI18next)
  .init({
    interpolation: {
      escapeValue: false,
    },
    backend: {
      loadPath: "/locales/{{lng}}.json",
    },
  });

export { i18n };

Voir aussi

  • Configuration - Configuration du Provider
  • Hooks - Documentation de useIntl
  • Thèmes - Personnalisation visuelle
Previous Next

Made with ❤ by CAP-REL · SmartMaker · GNU AGPL v3+
Code source · Faire un don
SmarMaker - Documentation
Traductions de cette page:
  • Français
  • Deutsch
  • English
  • Español
  • Italiano
  • Nederlands

Table of Contents

Table des matières

  • Traductions
    • Configuration rapide
      • Fichiers de traduction
      • Structure d'un fichier
      • Configuration du Provider
    • Utilisation avec useIntl
      • Hook useIntl
      • Propriétés de useIntl
    • Exemple complet : Page de login
    • Sélecteur de langue
      • Composant simple
      • Avec Select SmartCommon
    • Formatage avancé
      • Dates
      • Nombres et devises
    • Interpolation et pluriels
      • Interpolation
      • Pluriels
    • Organisation avancée
      • Namespaces
      • Prefix automatique
    • Configuration manuelle (sans SmartCommon)
    • Voir aussi
  • SmartAuth
  • SmartMaker - Back (PHP)
    • Mapping Dolibarr - React
  • SmartMaker - Front (React)
    • Animations de pages
    • Architecture
    • Astuces
    • Calendar
    • Composants et pages
    • Configuration du Provider
    • Debug et Logs
    • Hooks SmartCommon
    • PWA (Progressive Web App)
    • Requêtes API
    • Routage
    • SmartCommon
    • Stockage de données
    • Thèmes
    • Traductions
  • HowTo - Pas à pas - Votre première application
    • Développement PHP (back)
    • Développement React (front)
    • Première étape : Module Builder Dolibarr
    • SmartAuth
    • SmartBoot : Un "squelette" quasiment prêt à l'emploi
  • Formation SmartMaker
    • Module 1 : Fondamentaux JavaScript ES6+
      • Chapitre 1 : Variables et Scope
      • Chapitre 2 : Fonctions
      • Chapitre 3 : Programmation Asynchrone
      • Chapitre 4 : Modules ES6
    • Module 2 : Introduction à React
      • Chapitre 1 : Philosophie React
      • Chapitre 2 : JSX
      • Chapitre 3 : Composants
    • Module 3 : Hooks React Fondamentaux
      • Chapitre 1 : useState
      • Chapitre 2 : useEffect
      • Chapitre 3 : useRef
      • Chapitre 4 : useContext
    • Module 4 : React Avancé
      • Chapitre 1 : useCallback et useMemo
      • Chapitre 2 : Custom Hooks
      • Chapitre 3 : Redux et Redux Toolkit
    • Module 5 : Architecture SmartMaker
      • Chapitre 1 : Structure du projet
      • Chapitre 2 : Configuration
      • Chapitre 3 : Flux de données
    • Module 6 : SmartCommon - Composants
      • Chapitre 1 : Mise en page
      • Chapitre 2 : Navigation
      • Chapitre 3 : Formulaires
      • Chapitre 4 : Affichage
    • Module 7 : SmartCommon - Hooks
      • Chapitre 1 : useApi
      • Chapitre 2 : Gestion d'état
      • Chapitre 3 : Hooks utilitaires
    • Module 8 : Backend API (PHP)
      • Chapitre 1 : Routage
      • Chapitre 2 : Controllers
      • Chapitre 3 : Mappers
      • Extrafields et formulaires dynamiques
    • Module 9 : Intégration complète
      • Chapitre 1 : Backend
      • Chapitre 2 : Frontend
      • Chapitre 3 : Déploiement
    • Module 10 : Fonctionnalités avancées
      • Chapitre 1 : Mode offline
      • Chapitre 2 : Internationalisation (i18n)
      • Chapitre 3 : Autres fonctionnalités
    • Module 11 : Bonnes pratiques
  • Démonstration
  • Start
  • Composants et pages
  • Afficher le texte source
  • Anciennes révisions
  • Liens de retour
  • Haut de page