-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation.php
More file actions
48 lines (43 loc) · 1.44 KB
/
translation.php
File metadata and controls
48 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
session_start();
// Variable globale pour stocker les traductions
$translations = null;
// Fonction pour définir la langue
function setLanguage($lang) {
$_SESSION['lang'] = $lang;
}
// Fonction pour obtenir la langue actuelle
function getLanguage() {
if (isset($_GET['lang']) && in_array($_GET['lang'], ['en', 'fr'])) {
setLanguage($_GET['lang']);
header("Location: " . strtok($_SERVER['REQUEST_URI'], '?')); // Supprime les paramètres GET
exit;
}
return isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en';
}
// Fonction pour charger les traductions depuis le fichier JSON
function loadTranslations() {
global $translations;
$lang = getLanguage();
$jsonFile = basename($_SERVER['PHP_SELF']) . ".$lang.json";
if (file_exists($jsonFile)) {
$translations = json_decode(file_get_contents($jsonFile), true);
} else {
// Charger les traductions en anglais par défaut
$jsonFile = basename($_SERVER['PHP_SELF']) . ".en.json";
if (file_exists($jsonFile)) {
$translations = json_decode(file_get_contents($jsonFile), true);
} else {
$translations = [];
}
}
}
// Fonction pour traduire le texte
function translate($key) {
global $translations;
if ($translations === null) {
loadTranslations();
}
return isset($translations[$key]) ? $translations[$key] : $key;
}
?>