⚙️ Mécanisme d'affichage des traductions et son test

pull/188/head
Laurent Bossavit 2018-02-26 14:34:45 +01:00 committed by Mael
parent eb9cfd96f9
commit 896bf43da3
2 changed files with 59 additions and 6 deletions

View File

@ -6,7 +6,10 @@ import {
contains,
without,
concat,
length
length,
reduce,
assoc,
map
} from 'ramda'
import { combineReducers } from 'redux'
import reduceReducers from 'reduce-reducers'
@ -14,6 +17,7 @@ import { reducer as formReducer, formValueSelector } from 'redux-form'
import {
rules,
enrichRule,
findRuleByName,
collectDefaults,
formatInputs
@ -31,6 +35,8 @@ import { analyseMany, parseAll } from 'Engine/traverse'
import ReactPiwik from 'Components/Tracker'
import translations from 'Règles/externalized.yaml'
// assume "wraps" a given situation function with one that overrides its values with
// the given assumptions
export let assume = (evaluator, assumptions) => state => name => {
@ -52,12 +58,31 @@ let nextWithoutDefaults = (
return { currentQuestion: head(nextSteps), nextSteps }
}
export let translateAll = (translations, flatRules) => {
let translationsOf = rule => translations[enrichRule(rule).dottedName],
translateProp = (lang, translation) => (rule, prop) => {
let propTrans = translation[prop + '.' + lang]
return propTrans ? assoc(prop, propTrans, rule) : rule
},
translateRule = (lang, translations, props) => rule => {
let ruleTrans = translationsOf(rule)
return ruleTrans
? reduce(translateProp(lang, ruleTrans), rule, props)
: rule
}
let targets = ['titre', 'description', 'question', 'sous-question']
return map(translateRule('en', translations, targets), flatRules)
}
export let reduceSteps = (tracker, flatRules, answerSource) => (
state,
action
) => {
// Optimization - don't parse on each analysis
if (!state.parsedRules) state.parsedRules = parseAll(flatRules)
if (!state.parsedRules)
state.parsedRules = parseAll(translateAll(translations, flatRules))
if (
![START_CONVERSATION, STEP_ACTION, 'USER_INPUT_UPDATE'].includes(
@ -102,12 +127,12 @@ export let reduceSteps = (tracker, flatRules, answerSource) => (
done,
...(done && assumptionsMade
? // The simulation is "over" - except we can now fill in extra questions
// where the answers were previously given default reasonable assumptions
nextWithoutDefaults(state, analysis, targetNames, intermediateSituation)
// where the answers were previously given default reasonable assumptions
nextWithoutDefaults(state, analysis, targetNames, intermediateSituation)
: {
currentQuestion: head(nextWithDefaults),
nextSteps: nextWithDefaults
})
})
}
if (action.type == START_CONVERSATION) {

View File

@ -7,7 +7,7 @@ import {
getObjectives
} from '../source/engine/generateQuestions'
import { reduceSteps } from '../source/reducers'
import { reduceSteps, translateAll } from '../source/reducers'
import yaml from 'js-yaml'
import dedent from 'dedent-js'
@ -140,3 +140,31 @@ describe('fold', function() {
expect(step2.currentQuestion).to.equal('cadre')
})
})
describe('translateAll', function() {
it('should translate flat rules', function() {
let rules = [{
"espace":"foo",
"nom":"bar",
"titre":"Titre",
"description":"Description",
"question":"Question",
"sous-question":"Sous Question",
}]
let translations = {
"foo . bar":{
"titre.en":"TITRE",
"description.en":"DESC",
"question.en":"QUEST",
"sous-question.en":"SOUSQ",
}
}
let result = translateAll(translations, rules)
expect(result[0]).to.have.property("titre","TITRE")
expect(result[0]).to.have.property("description","DESC")
expect(result[0]).to.have.property("question","QUEST")
expect(result[0]).to.have.property("sous-question","SOUSQ")
})
})