-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage-manager.php
More file actions
executable file
·53 lines (41 loc) · 1.52 KB
/
language-manager.php
File metadata and controls
executable file
·53 lines (41 loc) · 1.52 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
49
50
51
52
53
<?php
require_once 'config/config.php';
require_once 'classes/UserProfile.php';
Config::load();
// Verificar que sea una petición AJAX
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || $_SERVER['HTTP_X_REQUESTED_WITH'] !== 'XMLHttpRequest') {
header('HTTP/1.1 400 Bad Request');
exit('Solo peticiones AJAX');
}
header('Content-Type: application/json');
$response = ['success' => false, 'message' => ''];
try {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
if (!$input || !isset($input['language'])) {
throw new Exception('Idioma no especificado');
}
$language = $input['language'];
// Validar idioma
if (!in_array($language, ['es', 'en', 'de'])) {
throw new Exception('Idioma no válido');
}
// Cambiar idioma en la clase I18n
$i18n = I18n::getInstance();
if ($i18n->setLanguage($language)) {
$response['success'] = true;
$response['message'] = 'Idioma cambiado correctamente';
$response['data'] = [
'language' => $language,
'language_name' => $i18n->getCurrentLanguageName()
];
} else {
throw new Exception('Error al cambiar el idioma');
}
} else {
throw new Exception('Método no permitido');
}
} catch (Exception $e) {
$response['message'] = $e->getMessage();
}
echo json_encode($response);