aimpress-chatbot/translator.js
2025-04-27 18:04:34 +01:00

21 lines
No EOL
880 B
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const axios = require('axios');
const subscriptionKey = '54FyVMKaRBATXAy2QKhEL1USlgrIlAGVHJVO9rjynnKGcZA0S2KtJQQJ99BDAC5RqLJXJ3w3AAAbACOGuFGT';
const endpoint = 'https://aimpress-translator.cognitiveservices.azure.com/';
async function translateText(text, targetLanguage) {
try {
const response = await axios.post(`${endpoint}/translate?api-version=3.0&to=${targetLanguage}`, [{ Text: text }], {
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-Type': 'application/json'
}
});
return response.data[0].translations[0].text;
} catch (error) {
console.error('Translation error:', error.response ? error.response.data : error.message);
return text; // В случае ошибки — вернуть оригинал
}
}
module.exports = { translateText };