2018-02-26 10:55:55 +00:00
|
|
|
var yaml = require('js-yaml')
|
|
|
|
var fs = require('fs')
|
2018-03-05 16:00:47 +00:00
|
|
|
var translate = require('google-translate-api')
|
|
|
|
var path = require('path')
|
|
|
|
let R = require('ramda')
|
2018-02-26 10:55:55 +00:00
|
|
|
|
2018-03-06 16:16:30 +00:00
|
|
|
let externalizationPath = 'source/règles/externalized.yaml'
|
|
|
|
// Test the translation agent
|
2018-03-05 16:00:47 +00:00
|
|
|
translate(
|
|
|
|
'Bonjour, je suis votre agent traducteur. Je vais traduire votre application, puis ensuite dominer le monde',
|
|
|
|
{ from: 'fr', to: 'en' }
|
|
|
|
)
|
|
|
|
.then(res => {
|
|
|
|
console.log(res.text)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
})
|
2018-02-26 12:47:29 +00:00
|
|
|
|
2018-03-06 16:16:30 +00:00
|
|
|
let rules = yaml.safeLoad(
|
2018-03-05 16:00:47 +00:00
|
|
|
fs.readFileSync(path.resolve('source/règles/base.yaml'), 'utf-8')
|
|
|
|
)
|
2018-02-26 10:55:55 +00:00
|
|
|
|
2018-03-06 16:16:30 +00:00
|
|
|
let currentExternalization = yaml.safeLoad(
|
|
|
|
fs.readFileSync(path.resolve(externalizationPath), 'utf-8')
|
|
|
|
)
|
|
|
|
|
2018-03-05 16:00:47 +00:00
|
|
|
let attributesToExternalize = [
|
|
|
|
'titre',
|
|
|
|
'description',
|
|
|
|
'question',
|
|
|
|
'sous-question',
|
2018-08-30 09:11:34 +00:00
|
|
|
'résumé',
|
|
|
|
'suggestions'
|
2018-03-05 16:00:47 +00:00
|
|
|
]
|
2018-02-26 10:55:55 +00:00
|
|
|
|
2018-03-06 16:16:30 +00:00
|
|
|
let buildDottedName = rule =>
|
|
|
|
rule.espace ? rule.espace + ' . ' + rule.nom : rule.nom
|
|
|
|
|
2018-03-05 16:00:47 +00:00
|
|
|
let promises = rules
|
|
|
|
.map(rule => (!rule.titre ? { ...rule, titre: rule.nom } : rule))
|
|
|
|
.map(rule =>
|
|
|
|
Promise.all(
|
|
|
|
R.toPairs(rule).map(([k, v]) => {
|
|
|
|
let attrToTranslate = attributesToExternalize.find(R.equals(k))
|
|
|
|
if (!attrToTranslate) return Promise.resolve({})
|
2018-03-06 16:16:30 +00:00
|
|
|
let enTrad = attrToTranslate + '.en',
|
|
|
|
frTrad = attrToTranslate + '.fr'
|
|
|
|
|
|
|
|
let currentTranslation = currentExternalization[buildDottedName(rule)]
|
|
|
|
//Check if a human traduction exists already for this attribute
|
2018-08-30 09:11:34 +00:00
|
|
|
if (currentTranslation && currentTranslation[enTrad])
|
2018-03-06 16:16:30 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
[enTrad]: currentTranslation[enTrad],
|
|
|
|
[frTrad]: v
|
|
|
|
})
|
2018-03-05 16:00:47 +00:00
|
|
|
return translate(v, { from: 'fr', to: 'en' }).then(({ text }) => ({
|
2018-03-06 16:16:30 +00:00
|
|
|
[frTrad]: v,
|
|
|
|
// Mark auto translated text with a double tild symbol
|
|
|
|
[enTrad]: '~~' + text
|
2018-03-05 16:00:47 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
).then(attributes => {
|
2018-03-06 16:16:30 +00:00
|
|
|
let id = buildDottedName(rule)
|
2018-03-05 16:00:47 +00:00
|
|
|
return { [id]: R.mergeAll(attributes) }
|
|
|
|
})
|
|
|
|
)
|
2018-02-26 12:47:29 +00:00
|
|
|
|
2018-03-05 16:00:47 +00:00
|
|
|
Promise.all(promises).then(resolved =>
|
2018-03-06 16:16:30 +00:00
|
|
|
fs.writeFileSync(externalizationPath, yaml.safeDump(R.mergeAll(resolved)))
|
2018-02-26 10:55:55 +00:00
|
|
|
)
|