Question T13699
Visible to All Users

Localize Expressions

created 2 years ago

Hello,

I'm trying to create a dynamic matrix with a column of type expression that displays clues based on the selection made in the same row. Below is a MWE of this functionality. I'm using the expression iif({row.Selection}=='showComment', 'SHOWN', '') to show the clue based on the selection.

What is the best way to translate the string 'SHOWN' in this case? I tried reading the value of another (hidden) question that can be translated, but wasn't able to access the title. I also thought of enabling localization for expression fields through customizations in JS, but that would require copying the whole expression each time a change is made to it.

I'd be grateful for any suggestions.

Thanks,

Lucas

JSON
{ "logoPosition": "right", "pages": [ { "name": "page1", "elements": [ { "type": "matrixdynamic", "name": "question1", "columns": [ { "name": "Selection", "cellType": "dropdown", "choices": [ "showComment" ] }, { "name": "Comment", "cellType": "expression", "expression": "iif({row.Selection}=='showComment', 'SHOWN', '')" } ] } ] } ] }
Show previous comments (4)

    Hello Lucas,
    Thank you for the update. If you wish to allow users to define the column text and translate it, I would suggest that you create a custom localizable property for your Matrix question. Check the Add Custom Properties to the Property Grid documentation section for more information on how custom property settings.

    So, you may add a custom Matrix property and define different translations for it. A property will be available in a Survey Creator. To display a custom property value in a row's column, register a custom function. You should be able to access a survey instance within a custom function as this.survey.

    View Demo

    JSON
    import { Serializer, FunctionFactory } from "survey-core"; function getCommentText(params) { let commentText = this.survey.getQuestionByName("question1") .matrixCommentText; return commentText; } FunctionFactory.Instance.register("getCommentText", getCommentText); Serializer.addProperty("question", { name: "matrixCommentText", displayName: "Comment Text", type: "text", isLocalizable: true, category: "general" }); ... // Survey JSON { "locale": "de", "logoPosition": "right", "pages": [ { "name": "page1", "elements": [ { "type": "matrixdynamic", "name": "question1", "matrixCommentText": { "default": "SHOWN", "fr": "MONTRÉ", "de": "GETOOND" }, "columns": [ { "name": "Selection", "cellType": "dropdown", "choices": [ "showComment" ] }, { "name": "Comment", "cellType": "expression", "expression": "iif({row.Selection}=='showComment', getCommentText(), '')" } ] } ] } ] }

    Please let me know if this option works for you.

    LF LF
    Lucas Forster a year ago

      Hello Jane,

      Thanks for suggesting this alternative involving a custom property.

      However, I'm sticking with the custom function since I need an approach that can be applied to any future questions.
      (Also I found out that I have access to model which solves the problem of this being undefined.)

      I consider this ticket solved, but posted a follow-up on how my current solution may be optimized, as it isn't limited to localizing anymore, but generally applies to defining custom functions:
      https://surveyjs.answerdesk.io/ticket/details/t13837

      Thanks,

      Lucas

      AT AT
      Andrew Telnov a year ago

        Hello Lucas,
        this should not return undefined in the function. You should access survey as this.survey and if you are using an expression in question object then this.question.

        Thank you,
        Andrew
        SurveyJS Team