Files
flux-srl/src/lib/aiTranslator.ts
T
davidherran fc24313f15
Deploy to VPS / deploy (push) Has been cancelled
production: docker + nginx config for rf-flux.com
2026-03-20 13:46:05 -05:00

53 lines
2.1 KiB
TypeScript

import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
/**
* Motor de traducción impulsado por Vercel AI SDK y OpenAI.
* Usa generateText para evitar bugs de compatibilidad con Zod.
* @param content Objeto con los textos a traducir. Ej: { title: "...", content: "..." }
* @returns Objeto con los idiomas y sus traducciones
*/
export async function translateContentForCMS(content: Record<string, string>) {
try {
const { text } = await generateText({
model: openai('gpt-4o'),
system: `You are an elite technical translator for FLUX, a premium brand of Radio Frequency (RF) industrial machinery.
Your task is to translate the user's JSON content into 4 specific locales:
1. 'it': Standard Professional Italian.
2. 'vec': Venetian dialect (from Bassano del Grappa). Maintain a proud, industrial, and authentic tone.
3. 'es': Professional Spanish (Neutral/Global).
4. 'de': Professional Industrial German.
CRITICAL RULES:
- NEVER translate Markdown syntax (#, **, *, >, |---|).
- NEVER translate URLs, file paths (like /cases/img.jpg), or code blocks.
- NEVER translate technical acronyms like "RF", "kW", "MHz", "FLUX".
- Keep the exact same JSON key names as the input.
OUTPUT FORMAT:
You MUST return ONLY a raw, valid JSON object. Do not wrap it in \`\`\`json blocks. No pleasantries.
The output must strictly follow this structure:
{
"it": { "key1": "translated text..." },
"vec": { "key1": "translated text..." },
"es": { "key1": "translated text..." },
"de": { "key1": "translated text..." }
}`,
prompt: JSON.stringify(content),
});
// Limpiamos el texto por si GPT-4o decide ponerle "```json" alrededor
const cleanedText = text.replace(/```json/g, '').replace(/```/g, '').trim();
// Convertimos la respuesta de la IA en un objeto real de Javascript
const parsedObject = JSON.parse(cleanedText);
return parsedObject;
} catch (error) {
console.error("Error in AI Translation:", error);
return null;
}
}