Skip to main content

Internationalization

In a survey definition, textual contents are provided using a special structure called Localized text. This structure embeds texts for each language.

An example of Localized text :

{
"code":"language-code",
"parts: [
"Age"
]
}

Note that the parts field is an array, which can contains several entries. Each entry can be a string (literal text), a number or an ExpressionArg.

An Expression can be used to embeds dynamic text in the translation (the textual value is computed using an expression).

Common example is to show the text containing the duration from a response of a question. We can take the example of the age question.

The age is collected from an Year-Month component, to improve input validation we want to show the age (in years corresponding to the entered date). To do this we can defined a Localized text containing the expression calculating the age from the entered data in the year-month component.

Let's say the question is named 'survey.age' and the date input has the component key 'date'

{
"code":"en"
"parts":[
"Your age is",
{
"dtype": "exp",
"exp": {
"name": "dateResponseDiffFromNow",
"data": [
{"dtype": "str", "str": "survey.age"},
{"dtype": "str", "str": "rg.date"},
{"dtype": "str", "str": "years"},
{"dtype": "num", "num": 1 }
]
}
},
"years old"
]
}

Using a function call-like representation, this expression is :

    dateResponseDiffFromNow('survey.age', 'rg.date', 'years', 1)

dateResponseDiffFromNow compute the time difference from the response in a component (identified by the 2 first arguments, question key survey.age and component key rg.date inside this question), the time unit years and the direction (1=always output a positive duration) It's described in Expressions

Fortunately you dont have to write such a json, it's possible to use case-editor-tools library to generate such expression:

import { SurveyEngine  } from "case-editor-tools/surveys";
SurveyEngine.dateResponseDiffFromNow("survey.age","rg.date", "years", true)