false, 'error' => __('you_do_not_have_permission_to_manage_languages')], 403); } $input = json_decode(file_get_contents('php://input'), true); if (isset($input['action'])) { try { if ($input['action'] === 'toggle' && isset($input['id'])) { $stmt = $db->prepare("UPDATE idiomas SET activo = NOT activo WHERE id = ?"); $stmt->execute([$input['id']]); jsonResponse(['success' => true]); } elseif ($input['action'] === 'update_flag' && isset($input['id'])) { $flag = $input['flag'] ?? null; $stmt = $db->prepare("UPDATE idiomas SET bandera = ? WHERE id = ?"); $stmt->execute([$flag, $input['id']]); jsonResponse(['success' => true]); } elseif ($input['action'] === 'sync') { // 1. Obtener idiomas de LibreTranslate $ltUrl = $_ENV['LIBRETRANSLATE_URL'] ?? getenv('LIBRETRANSLATE_URL') ?? 'http://10.10.4.17:5000'; $ch = curl_init("$ltUrl/languages"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200 || !$response) { throw new Exception("No se pudo conectar con LibreTranslate ($ltUrl)"); } $languages = json_decode($response, true); if (!is_array($languages)) { throw new Exception("Respuesta inválida de LibreTranslate"); } // 2. Actualizar base de datos $stmt = $db->prepare(" INSERT INTO idiomas (codigo, nombre, nombre_nativo, activo) VALUES (?, ?, ?, 1) ON DUPLICATE KEY UPDATE nombre = VALUES(nombre), nombre_nativo = VALUES(nombre_nativo) "); $count = 0; foreach ($languages as $lang) { // LibreTranslate devuelve: code, name, targets // No siempre devuelve nombre nativo, usaremos el nombre en inglés por defecto $stmt->execute([ $lang['code'], $lang['name'], $lang['name'], // Usamos el mismo nombre ya que LT no siempre da el nativo en /languages ]); $count++; } jsonResponse(['success' => true, 'message' => str_replace('{count}', $count, __('synced_languages'))]); } } catch (Exception $e) { jsonResponse(['success' => false, 'error' => $e->getMessage()], 500); } } exit; } // Obtener idiomas $stmt = $db->query("SELECT * FROM idiomas ORDER BY nombre ASC"); $idiomas = $stmt->fetchAll(); // URL de LibreTranslate desde .env $libreTranslateUrl = $_ENV['LIBRETRANSLATE_URL'] ?? getenv('LIBRETRANSLATE_URL') ?? 'http://10.10.4.17:5000'; ?>
|
().