2 Commits
Author SHA1 Message Date
cedric 34a54eaae9 feat: add pt-BR/ja/ko translation languages
Déploiement API BETA / build (push) Successful in 2m12s
Déploiement API PROD / build (push) Successful in 2m9s
Déploiement API BETA / deploy (push) Successful in 47s
Déploiement API PROD / deploy (push) Successful in 1m0s
2026-07-03 13:40:22 +04:00
cedric 2003948a9a refactor: DeepL client uses fetch instead of axios 2026-07-03 13:34:38 +04:00
3 changed files with 47 additions and 40 deletions
+35 -40
View File
@@ -1,6 +1,5 @@
'use strict';
const qs = require('qs')
const axios = require('axios')
const Diff = require('diff')
const { createCoreService } = require('@strapi/strapi').factories;
@@ -8,10 +7,13 @@ const { ApplicationError } = require("@strapi/utils").errors
const LANG_MAP = {
fr: { field: 'francais', targetLang: 'fr', userPrompt: 'Tradui an fransé' },
en: { field: 'anglais', targetLang: 'en', userPrompt: 'Translate to English' },
es: { field: 'espagnol', targetLang: 'es', userPrompt: 'Traduce al español' },
de: { field: 'allemand', targetLang: 'de', userPrompt: 'Übersetze auf Deutsch' },
it: { field: 'italien', targetLang: 'it', userPrompt: 'Traduci in italiano' },
en: { field: 'anglais', targetLang: 'en', userPrompt: 'Translate to English', deeplTarget: 'EN', suffix: '\n\n (Translated by DeepL)' },
es: { field: 'espagnol', targetLang: 'es', userPrompt: 'Traduce al español', deeplTarget: 'ES', suffix: '\n\n (Traducido por DeepL)' },
de: { field: 'allemand', targetLang: 'de', userPrompt: 'Übersetze auf Deutsch', deeplTarget: 'DE', suffix: '\n\n (Übersetzt von DeepL)' },
it: { field: 'italien', targetLang: 'it', userPrompt: 'Traduci in italiano', deeplTarget: 'IT', suffix: '\n\n (Tradotto da DeepL)' },
pt: { field: 'portugais', targetLang: 'pt', userPrompt: 'Traduza para o português', deeplTarget: 'PT-BR', suffix: '\n\n (Traduzido pela DeepL)' },
ja: { field: 'japonais', targetLang: 'ja', userPrompt: '日本語に翻訳して', deeplTarget: 'JA', suffix: '\n\n (DeepLによる翻訳)' },
ko: { field: 'coreen', targetLang: 'ko', userPrompt: '한국어로 번역해줘', deeplTarget: 'KO', suffix: '\n\n (DeepL 번역)' },
}
const ALL_LANGS = Object.keys(LANG_MAP)
@@ -52,50 +54,47 @@ class Translator {
}
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, {
text: Array.isArray(text) ? text : [text],
source_lang: origin,
target_lang: target,
}, {
const response = await fetch(this.urlRequest, {
method: 'POST',
headers: {
Authorization: `DeepL-Auth-Key ${this.deeplKey}`,
'Content-Type': 'application/json'
}
},
body: JSON.stringify({
text: Array.isArray(text) ? text : [text],
source_lang: origin,
target_lang: target,
})
})
return result.data
} catch (error) {
console.error('DeepL error:', error?.response?.data || error);
}
if (!response.ok) {
const body = await response.text()
console.error('DeepL error:', body)
throw new Error(`DeepL ${response.status}: ${body}`)
}
return await response.json()
}
}
const translator = new Translator()
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
},
async translateLyrics(parolesFR) {
const anglais = await this.translate('FR', 'EN', parolesFR)
const espagnol = await this.translate('FR', 'ES', parolesFR)
const allemand = await this.translate('FR', 'DE', parolesFR)
const italien = await this.translate('FR', 'IT', parolesFR)
const result = { francais: parolesFR }
return {
francais: parolesFR,
anglais: anglais + '\n\n (Translated by DeepL)',
espagnol: espagnol + '\n\n (Traducido por DeepL)',
allemand: allemand + '\n\n (Übersetzt von DeepL)',
italien: italien + '\n\n (Tradotto da DeepL)'
for (const lang of ALL_LANGS) {
if (lang === 'fr') continue
const { field, deeplTarget, suffix } = LANG_MAP[lang]
const translated = await this.translate('FR', deeplTarget, parolesFR)
result[field] = translated + suffix
}
return result
},
validateParoles(titre, transcription) {
if (!titre || titre.trim().length === 0) {
@@ -199,12 +198,9 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
},
async bulkTranslateMissing() {
const TARGET_LANGS = [
{ lang: 'en', field: 'anglais', deeplTarget: 'EN', suffix: '\n\n(Translated by DeepL)' },
{ lang: 'es', field: 'espagnol', deeplTarget: 'ES', suffix: '\n\n(Traducido por DeepL)' },
{ lang: 'de', field: 'allemand', deeplTarget: 'DE', suffix: '\n\n(Übersetzt von DeepL)' },
{ lang: 'it', field: 'italien', deeplTarget: 'IT', suffix: '\n\n(Tradotto da DeepL)' },
]
const TARGET_LANGS = ALL_LANGS
.filter(lang => lang !== 'fr')
.map(lang => ({ lang, ...LANG_MAP[lang] }))
const pageSize = 100
let start = 0
@@ -222,7 +218,6 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
start += pageSize
}
const translator = new Translator()
const translated = []
const skipped = []
const errors = []
+9
View File
@@ -21,6 +21,15 @@
},
"italien": {
"type": "richtext"
},
"portugais": {
"type": "richtext"
},
"japonais": {
"type": "richtext"
},
"coreen": {
"type": "richtext"
}
}
}
+3
View File
@@ -93,9 +93,12 @@ export interface TradTraductions extends Struct.ComponentSchema {
attributes: {
allemand: Schema.Attribute.RichText;
anglais: Schema.Attribute.RichText;
coreen: Schema.Attribute.RichText;
espagnol: Schema.Attribute.RichText;
francais: Schema.Attribute.RichText;
italien: Schema.Attribute.RichText;
japonais: Schema.Attribute.RichText;
portugais: Schema.Attribute.RichText;
};
}