Translating piped text value with JavaScript | XM Community
Skip to main content

I am trying to have a piped text translated from English to another the language selected by the survey on starting the survey using JavaScript. 

I put together this script below but it failed. Any idea if this is possible? or ideas as to what I might be missing? 

Thanks in advance.

 

Qualtrics.SurveyEngine.addOnload(function() {
    // Get the selected language choice
    var selectedLanguage = "${e://Field/LanguageChoice}";

    // Get the current value of the piped text
    var pipedTextValue = "${e://Field/added%20info}";

    // Trim whitespace from the piped text value
    pipedTextValue = pipedTextValue.trim();

    // Define the specific text to search for
    var searchText = "This is the current text.";

    // Check if the piped text contains the specific text
    if (pipedTextValue.includes(searchText)) {
        // Check if the selected language is French
        if (selectedLanguage === "French") {
            // Replace the content of the preferenceInsight element with the French translation
            document.getElementById("preferenceInsight").innerText = "French translation of the text";
        }
    } else {
        // Log a message to the console for debugging if the specific text is not found
        console.log("The specific text was not found in the piped text value:", pipedTextValue);
    }
});
</script>
 

Hi @xelaO Just a suggestion, you can achieve this via embedded data and branch logics as well built in the survey flow.


Hi @xelaO,

I got few ideas that might help. 

  • Try to use addOnReady instead of addOnLoad as I am not sure if all data is already available while loading the screen
  • Fetch the language via the Q_lang element
  • Fetch the embedded data via Qualtrics.SurveyEngine.getEmbeddedData

So it should be something like this: 

Qualtrics.SurveyEngine.addOnReady(function() {
// Get the selected language choice
var selectedLanguage = document.getElementById("Q_lang").value;

// Get the current value of the piped text
var pipedTextValue = Qualtrics.SurveyEngine.getEmbeddedData( 'addedinfo' );

// Trim whitespace from the piped text value
pipedTextValue = pipedTextValue.trim();

// Define the specific text to search for
var searchText = "This is the current text.";

// Check if the piped text contains the specific text
if (pipedTextValue.indexOf(searchText) !== -1) {
// Check if the selected language is French
if (selectedLanguage === "French") {
// Replace the content of the preferenceInsight element with the French translation
document.getElementById("preferenceInsight").innerText = "French translation of the text";
}
} else {
// Log a message to the console for debugging if the specific text is not found
console.log("The specific text was not found in the piped text value:", pipedTextValue);
}
});

Please check the code I provided: 

  • Are the correct embedded data names used (addedinfo)? 
  • Is the language code really “French” and not “FR”?
  • Either you place the code in some question’s JavaScript or you place it in the footer HTML code in the settings

Hope this helps you.

Best
Christian 


Hi @xelaO Just a suggestion, you can achieve this via embedded data and branch logics as well built in the survey flow.

Hi, @omkarkewat, your suggestion worked. I created a dropdown to allow survey takers select a language and based on this language and branching logic, I was able to replace the piped text with the desired translated language.

Would it possible to use this same drop down to dynamically call translated versions of the survey I have available? 


My updated comment below.


My updated comment below


Hi @xelaO By ‘dynamically calling translated versions of the survey’, I’m anticipating that you want to show a response summary of the survey in that particular translated language at the very end. If yes, please go through this page.
Is this you are looking for?


Hi @xelaO By ‘dynamically calling translated versions of the survey’, I’m anticipating that you want to show a response summary of the survey in that particular translated language at the very end. If yes, please go through this page.
Is this you are looking for?

Hi @omkarkewat … Really helpful!!! I got it working last night. 


Hi @xelaO,

I got few ideas that might help. 

  • Try to use addOnReady instead of addOnLoad as I am not sure if all data is already available while loading the screen
  • Fetch the language via the Q_lang element
  • Fetch the embedded data via Qualtrics.SurveyEngine.getEmbeddedData

So it should be something like this: 

Qualtrics.SurveyEngine.addOnReady(function() {
// Get the selected language choice
var selectedLanguage = document.getElementById("Q_lang").value;

// Get the current value of the piped text
var pipedTextValue = Qualtrics.SurveyEngine.getEmbeddedData( 'addedinfo' );

// Trim whitespace from the piped text value
pipedTextValue = pipedTextValue.trim();

// Define the specific text to search for
var searchText = "This is the current text.";

// Check if the piped text contains the specific text
if (pipedTextValue.indexOf(searchText) !== -1) {
// Check if the selected language is French
if (selectedLanguage === "French") {
// Replace the content of the preferenceInsight element with the French translation
document.getElementById("preferenceInsight").innerText = "French translation of the text";
}
} else {
// Log a message to the console for debugging if the specific text is not found
console.log("The specific text was not found in the piped text value:", pipedTextValue);
}
});

Please check the code I provided: 

  • Are the correct embedded data names used (addedinfo)? 
  • Is the language code really “French” and not “FR”?
  • Either you place the code in some question’s JavaScript or you place it in the footer HTML code in the settings

Hope this helps you.

Best
Christian 

I am still curious about this even though I found a workaround. You raised solid points in reference to some things I was doing wrong i.e. language code plus suggesting fetching with the Q_Language element. Hence, I will still play around with my initial code a bit out of curiosity to see if it had a chance of also solving my problem. Much appreciated.


Leave a Reply