mon-entreprise/source/externalize.js

56 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

var { safeLoad, safeDump } = require('js-yaml')
var fs = require('fs')
var path = require('path')
let R = require('ramda')
let externalizationPath = 'source/règles/externalized.yaml'
let rules = safeLoad(
fs.readFileSync(path.resolve('source/règles/base.yaml'), 'utf-8')
)
let currentExternalization = safeLoad(
fs.readFileSync(path.resolve(externalizationPath), 'utf-8')
)
let attributesToExternalize = [
'titre',
'description',
'question',
'résumé',
'suggestions',
'contrôles'
]
let buildDottedName = rule =>
rule.espace ? rule.espace + ' . ' + rule.nom : rule.nom
2019-04-09 17:01:59 +00:00
let resolved = rules
.map(rule => (!rule.titre ? { ...rule, titre: rule.nom } : rule))
2019-04-09 17:01:59 +00:00
.map(rule => ({
[buildDottedName(rule)]: R.mergeAll(
R.toPairs(rule)
.filter(([, v]) => !!v)
2019-04-09 17:01:59 +00:00
.map(([k, v]) => {
let attrToTranslate = attributesToExternalize.find(R.equals(k))
if (!attrToTranslate) return {}
let enTrad = attrToTranslate + '.en',
frTrad = attrToTranslate + '.fr'
let currentTranslation = currentExternalization[buildDottedName(rule)]
//Check if a human traduction exists already for this attribute
if (currentTranslation && currentTranslation[enTrad])
return {
[enTrad]: currentTranslation[enTrad],
[frTrad]: v
}
return {
[enTrad]: '!!' + v,
[frTrad]: v
2019-04-09 17:01:59 +00:00
}
})
)
}))
fs.writeFileSync(externalizationPath, safeDump(R.mergeAll(resolved)))