Aller au contenu principal

Langue

Configuration

Un système de multi-langue est possible via le framework.

Dans le dossier app/Lang/translation vous pouvez retrouver un fichier fr.php qui ressemble à un gros array comme ceci :

app/Lang/translations/fr.php
return [
'home' => [
'title' => 'Exemple de titre'
]
]

Pour une traduction par exempl en anglais il suffirait de créer un fichier en.php et coller ceci :

app/Lang/translations/en.php
return [
'home' => [
'title' => 'Example of title'
]
]

Pour d'autres lang il suffira de faire la meme chose pour toutes les langues souhaitez.

Utilisation dans le front

Rien de plus simple, il suffit via la méthode trans() d'appeler vos variables là où vous souhaitez ajouter votre texte :

<h1><?= trans('home.title') ?></h1>

Changement de langue

Pour changer de code vous pouvez insérer ce code où vous souhaitez dans votre code :

<form method="POST">
<?php if ($_SESSION['lang'] === 'en') : ?>
<div class="flex space-x-2">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-globe-icon lucide-globe"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg>
<button type="submit" name="lang" class="cursor-pointer" value="fr" title="change language to English">Fr</button>
</div>
<?php else : ?>
<div class="flex space-x-2 cursor-pointer">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-globe-icon lucide-globe"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg>
<button type="submit" name="lang" class="cursor-pointer" value="en" title="Changer la langue en français">En</button>
</div>
<?php endif; ?>
</form>
info

Ici nous avons un exemple pour une traduction entre deux langues, il est tout à fait possible d'en mettre bien plus et pour cela passer sur un dropdown.

Et y insérer ce code, pour récupérer le formulaire soumis, dans le même fichier :

if (!empty($_POST) && isset($_POST['lang'])) {
changeLanguage($_POST['lang']);
header('Location: ' . $_SERVER['REQUEST_URI']);
exit();
}