59 lines
1.7 KiB
PHP
Executable File
59 lines
1.7 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Translation Manager
|
|
*/
|
|
|
|
class TranslationManager {
|
|
private static $instance = null;
|
|
private $translations = [];
|
|
private $language = 'es'; // Idioma por defecto
|
|
|
|
private function __construct() {
|
|
// Obtener el idioma del usuario desde el JWT
|
|
if (class_exists('JWTAuth')) {
|
|
$userData = JWTAuth::getUserData();
|
|
if (isset($userData->idioma)) {
|
|
$this->language = $userData->idioma;
|
|
}
|
|
}
|
|
|
|
$this->loadTranslations();
|
|
}
|
|
|
|
public static function getInstance() {
|
|
if (self::$instance == null) {
|
|
self::$instance = new TranslationManager();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
private function loadTranslations() {
|
|
$filePath = __DIR__ . '/' . $this->language . '.json';
|
|
if (file_exists($filePath)) {
|
|
$json = file_get_contents($filePath);
|
|
$this->translations = json_decode($json, true);
|
|
} else {
|
|
// Si el archivo de idioma no existe, cargar español por defecto
|
|
$defaultFilePath = __DIR__ . '/es.json';
|
|
if (file_exists($defaultFilePath)) {
|
|
$json = file_get_contents($defaultFilePath);
|
|
$this->translations = json_decode($json, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function get($key, $default = null) {
|
|
return $this->translations[$key] ?? $default ?? $key;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Helper global para traducciones
|
|
* @param string $key La clave de traducción.
|
|
* @param string|null $default Un valor por defecto si la clave no se encuentra.
|
|
* @return string
|
|
*/
|
|
function __($key, $default = null) {
|
|
return TranslationManager::getInstance()->get($key, $default);
|
|
}
|