chairemobilite / evolution

Online survey platform for travel survey
MIT License
4 stars 9 forks source link

generator: Add the possibility to add extra fields to labels (for example count and gender) #606

Open tahini opened 3 months ago

tahini commented 3 months ago

Currently, the generator only accepts simple labels. If we want to add variables to the labels, eg. "Here follow questions about your trip to {{destination}}", we need to use a custom widget.

2 possible solutions (not mutually exclusive):

  1. Just support customLabels: like customConditionals and customValidations, these can be written by survey developers and be as complicated as necessary (and we do have very complicated labels in some surveys).
  2. Support variables in normal labels: we already have special cases for labels to integrate gendered suffix labels and persons count labels. We could add a column containing the paths of the responses to add, document that the labels can contain those named {{path after last dot}} and they can be added to the t function call.

Solution 1 is relatively straightforward given the other custom stuff we have. @samuel-duhaime any idea how we could implement solution 2 to support multiple variables from the excel file?

samuel-duhaime commented 3 months ago

For solution 2, I did something similar in generate_conditionals.py for relativePath (for example, when you want to get the relativePath of the group.)

# Check if any conditional has a path that contains "${relativePath}"
conditionals_has_path = any(
    "${relativePath}" in conditional["path"] for conditional in conditionals
)
declare_relative_path = f"{INDENT}const relativePath = path.substring(0, path.lastIndexOf('.'));"
tahini commented 3 months ago

So we could have an extra sheet name LabelVariables, with 3 columns:

  1. labelName: corresponds to the widget
  2. variableName: name of the variable in the text
  3. path: Path in the responses

If a widget has entries in the LabelVariables sheet, we replace the (t) => t(blabla) with

(t, interview) => t(blabla, {
    variableName: getResponse(interview, path),
});
samuel-duhaime commented 3 months ago

Yes, that could work!

samuel-duhaime commented 1 month ago

En passant, avec i18next, tu peux mettre le count en option à la fonction t et si count === 1, ça cherche le label avec le suffixe _one (et il y a plein d'autres features reliés à ça, mais c,est celui qu'on utilise le plus).

Par exemple, ici, tu aurais pu juste mettre t('household:age', { count: helper.countPersons(interview) }) au lieu du if/else et les deux libellés seraient age et age_one dans houshold.yml

from @tahini

We should add count like this.