21 lines
No EOL
880 B
JavaScript
21 lines
No EOL
880 B
JavaScript
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 }; |