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'; 'use strict';
const qs = require('qs') const qs = require('qs')
const axios = require('axios')
const Diff = require('diff') const Diff = require('diff')
const { createCoreService } = require('@strapi/strapi').factories; const { createCoreService } = require('@strapi/strapi').factories;
@@ -8,10 +7,13 @@ const { ApplicationError } = require("@strapi/utils").errors
const LANG_MAP = { const LANG_MAP = {
fr: { field: 'francais', targetLang: 'fr', userPrompt: 'Tradui an fransé' }, fr: { field: 'francais', targetLang: 'fr', userPrompt: 'Tradui an fransé' },
en: { field: 'anglais', targetLang: 'en', userPrompt: 'Translate to English' }, 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' }, 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' }, 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' }, 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) const ALL_LANGS = Object.keys(LANG_MAP)
@@ -52,50 +54,47 @@ class Translator {
} }
async get(origin, target, text) { async get(origin, target, text) {
try { const response = await fetch(this.urlRequest, {
const data = { method: 'POST',
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,
}, {
headers: { headers: {
Authorization: `DeepL-Auth-Key ${this.deeplKey}`, Authorization: `DeepL-Auth-Key ${this.deeplKey}`,
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} },
body: JSON.stringify({
text: Array.isArray(text) ? text : [text],
source_lang: origin,
target_lang: target,
})
}) })
return result.data if (!response.ok) {
} catch (error) { const body = await response.text()
console.error('DeepL error:', error?.response?.data || error); 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}) => ({ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
async translate(origin, target, text) { async translate(origin, target, text) {
const translator = new Translator()
const data = await translator.get(origin, target, text) const data = await translator.get(origin, target, text)
return data.translations[0].text return data.translations[0].text
}, },
async translateLyrics(parolesFR) { async translateLyrics(parolesFR) {
const anglais = await this.translate('FR', 'EN', parolesFR) const result = { francais: 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)
return { for (const lang of ALL_LANGS) {
francais: parolesFR, if (lang === 'fr') continue
anglais: anglais + '\n\n (Translated by DeepL)', const { field, deeplTarget, suffix } = LANG_MAP[lang]
espagnol: espagnol + '\n\n (Traducido por DeepL)', const translated = await this.translate('FR', deeplTarget, parolesFR)
allemand: allemand + '\n\n (Übersetzt von DeepL)', result[field] = translated + suffix
italien: italien + '\n\n (Tradotto da DeepL)'
} }
return result
}, },
validateParoles(titre, transcription) { validateParoles(titre, transcription) {
if (!titre || titre.trim().length === 0) { if (!titre || titre.trim().length === 0) {
@@ -199,12 +198,9 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
}, },
async bulkTranslateMissing() { async bulkTranslateMissing() {
const TARGET_LANGS = [ const TARGET_LANGS = ALL_LANGS
{ lang: 'en', field: 'anglais', deeplTarget: 'EN', suffix: '\n\n(Translated by DeepL)' }, .filter(lang => lang !== 'fr')
{ lang: 'es', field: 'espagnol', deeplTarget: 'ES', suffix: '\n\n(Traducido por DeepL)' }, .map(lang => ({ lang, ...LANG_MAP[lang] }))
{ 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 pageSize = 100 const pageSize = 100
let start = 0 let start = 0
@@ -222,7 +218,6 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({
start += pageSize start += pageSize
} }
const translator = new Translator()
const translated = [] const translated = []
const skipped = [] const skipped = []
const errors = [] const errors = []
+9
View File
@@ -21,6 +21,15 @@
}, },
"italien": { "italien": {
"type": "richtext" "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: { attributes: {
allemand: Schema.Attribute.RichText; allemand: Schema.Attribute.RichText;
anglais: Schema.Attribute.RichText; anglais: Schema.Attribute.RichText;
coreen: Schema.Attribute.RichText;
espagnol: Schema.Attribute.RichText; espagnol: Schema.Attribute.RichText;
francais: Schema.Attribute.RichText; francais: Schema.Attribute.RichText;
italien: Schema.Attribute.RichText; italien: Schema.Attribute.RichText;
japonais: Schema.Attribute.RichText;
portugais: Schema.Attribute.RichText;
}; };
} }