Files
api.pawol.nu/src/api/parole/services/parole.js
T

42 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-05-12 03:33:24 +04:00
'use strict';
2022-05-20 00:05:34 +04:00
const qs = require('qs')
const axios = require('axios')
2022-05-12 03:33:24 +04:00
const { createCoreService } = require('@strapi/strapi').factories;
2022-05-20 00:05:34 +04:00
class Translator {
constructor() {
this.deeplApi = process.env.DEEPL_URL || 'api-free.deepl.com'
this.deeplKey = process.env.DEEPL_KEY
this.urlRequest = `https://${this.deeplApi}/v2/translate`
}
async get(origin, target, text) {
try {
const data = {
auth_key: this.deeplKey,
source_lang: origin,
target_lang: target,
text
}
const result = await axios.post(this.urlRequest, qs.stringify(data), {
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
})
return result.data
} catch (error) {
console.log('error', error)
}
}
}
module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
async translate(origin, target, text) {
const translator = new Translator()
const data = await translator.get(origin, target, text)
return data.translations[0].text
}
}));