From 896bf43da303fb55cfdf2e324972b16329db0a5f Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Mon, 26 Feb 2018 14:34:45 +0100 Subject: [PATCH] =?UTF-8?q?:gear:=20M=C3=A9canisme=20d'affichage=20des=20t?= =?UTF-8?q?raductions=20et=20son=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/reducers.js | 35 ++++++++++++++++++++++++++++++----- test/reducers.test.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/source/reducers.js b/source/reducers.js index f9032f32c..75746c9d2 100644 --- a/source/reducers.js +++ b/source/reducers.js @@ -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) { diff --git a/test/reducers.test.js b/test/reducers.test.js index 921a93a22..1364f5c4c 100644 --- a/test/reducers.test.js +++ b/test/reducers.test.js @@ -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") + }) +})