How do I programmatically switch to another translation?

The translation that is used is determined by page load. There might be a situation where you want to change the translation used after the page has loaded. There is a survey function that can be called in the frontend to programmatically set the translation after page load.

When at least one survey has been triggered on the page, then the following function is also present:srv. The srv function has a method to set the language:

1 srv.setLanguage(<form_key>,<language>)

This method takes two parameters as input, the first is the form_key of the survey, and the second is the language you want to set.

You can find the survey’s key on the ‘Feedback forms’ page. Click on the icon with the three dots, and click on ‘Copy form key’.

Alternatively, an array of all surveys that are being loaded on a page is present on the srv function as well, it can be accessed here: srv.ALL_KEYS.

The second parameter's value should match the value used in the translations settings in the form builder:

1 srv.setLanguage(<form_key>,'fr')

If you want to switch back to the original language of the survey, you can use

srv.setLanguage(<form_key>, 'default')

Summary

  • To change to another translation after page load programmatically you can call srv.setLanguage with form_key and translation variable as parameters.

  • This function is only available when at least one survey is triggered on the page.

  • If you want to be able to switch back to the original language, this language should be added as a translation option as well.