Dynamically switch the page translation | XM Community

Dynamically switch the page translation

  • 16 April 2019
  • 3 replies
  • 155 views

Userlevel 5
Badge +13
If you wish to automatically change the language selection for the survey using javascript you can simply use:

jQuery("select[id='Q_lang']").val([language code here]); // change [language code here] to "ES" or whatever translation code you wish to switch to
jQuery("select[id='Q_lang']").trigger( "change" );

Below is a use case for changing the page translation based on the selected response to a question:

var qid=this.questionId; // current question ID
var objResponseOpt = jQuery("select[id='QR\\~"+qid+"']"); // current selection list object
// listen for changes to the selection list
objResponseOpt.on('change',objResponseOpt,function (e) {
responseChange(e);
});
function responseChange(e) {
if (typeof e.isTrigger == 'undefined') {
// change the language selector depending on selected list option
if (e.currentTarget.options.selectedIndex==1 || e.currentTarget.options.selectedIndex==4) {
jQuery("select[id='Q_lang']").val("ES");
jQuery("select[id='Q_lang']").trigger( "change" );
}
else {
jQuery("select[id='Q_lang']").val("EN");
jQuery("select[id='Q_lang']").trigger( "change" );
}
}
}

3 replies

Badge

I am trying to implement this in Question JS - so I can conditionalize the language set based on another passed through variable (and while I'm sure there's a way to do that in the jquery, I'm not the most savvy there). However, when I add the text there I'm getting an Unexpected Token error. Any thoughts?

Qualtrics.SurveyEngine.addOnload(function() {
   jQuery("select[id='Q_lang']").val("EN-GB"]);
});

I tried changing jQuery to $$ - I tried changing Q_lang to Q_language but nothing worked.

Badge

Aha - I had a rogue ] after the language - still trying to troubleshoot - will update here if I figure it out.

Badge

Hello: how would you do a conditional statement based on the language selected? I can get it to recognize the default language, but it doesn't apply to the language changed to. For example, if my default language was English, the proper output displays. If I change to Spanish using the following, it does not change.
var language = "This is English";
if(jQuery("select#Q_lang").val() == 'ES-ES') {  
var language = "This is Spanish";

jQuery('#demo').html(language);

Leave a Reply