The table below shows the original Chinese text in the first column and the English translation in the second column:
includeWeb("QScript Utility Functions");
includeWeb("QScript Selection Functions");
includeWeb("QScript Functions to Generate Outputs");
includeWeb("QScript R Output Functions");
includeWeb('Translation Languages');
function getVariableOrQuestionLabel(variable) {
if(/- Multi/.test(variable.question.variableSetStructure)) {
return variable.question.name + " " + variable.label;
} else {
return variable.label;
}
}
translateText();
function translateText() {
const allowed_types = ["Text", "Text - Multi"];
let selected_questions = selectInputQuestions(allowed_types);
if (!selected_questions)
return false;
let data_file = getDataFileFromQuestions(selected_questions);
let source_language;
let language_var_guid;
if (!inDisplayr()) {
let source_index = selectOne('Select the source language', ["English", "Specify with variable", "Auto-detect"].concat(TRANSLATION_LANGUAGES), null, 0);
if (source_index === 0)
source_language = "'English'";
else if (source_index === 1)
{
let candidate_variables = getVariables(data_file.questions).filter(function(x) { return(!x.question.isHidden) });
let source_language_var = selectOneVariableByNameAndLabel("Select the source language variable", candidate_variables, false);
source_language = "'Specify with variable'"
language_var_guid = source_language_var.guid;
} else if (source_index === 2) {
source_language = "'Auto-detect'";
} else
source_language = "'" + TRANSLATION_LANGUAGES[source_index - 3] + "'";
} else
source_language = "'Auto-detect'";
// Prompt user for output language
let output_index = selectOne('Select the output language (translated by Google Cloud Translation)', ["English"].concat(TRANSLATION_LANGUAGES), null, 0);
if (output_index === 0)
output_language = "'English'";
else
output_language = "'" + TRANSLATION_LANGUAGES[output_index - 1] + "'";
if (source_language == output_language)
{
log("The source language is the same as the output language. No translation has been performed.");
return false;
}
let r_code = `
source.lang <- get0("formSourceLanguageVariable", ifnotfound = formSourceLanguage)
apply(as.data.frame(formInputs, optional = TRUE), 2, flipTextAnalysis::Translate,
source.language = source.lang, target.language = formOutputLanguage)`;
let js_code = `
const TRANSLATION_LANGUAGES = ['${TRANSLATION_LANGUAGES.join("','")}'];
form.dropBox({name: 'formInputs',
label: 'Variables',
duplicates: true,
types: ['Variable:Text'],
multi:true,
prompt: 'Text variables to translate.'});
let source_language = form.comboBox({name: 'formSourceLanguage',
alternatives: ["Auto-detect", "English", "Specify with variable"].concat(TRANSLATION_LANGUAGES),
label: 'Source language',
prompt: 'Source language of input text. Use "Auto-detect" to automatically detect the language',
default_value: ${source_language}}).getValue();
if (source_language === "Specify with variable")
form.dropBox({name: 'formSourceLanguageVariable',
label: 'Source language variable',
types: ['Variable:Text,Categorical'],
multi:false,
prompt: 'Text variable containing the source language.'});
form.comboBox({name: 'formOutputLanguage',
alternatives: ["English"].concat(TRANSLATION_LANGUAGES),
label: 'Output language',
prompt: 'Language to use for output text.',
default_value: ${output_language}});
`;
let new_r_questions = [];
const structure_name = inDisplayr() ? "variable set" : "question";
for (var i = 0; i < selected_questions.length; i++)
{
let question = selected_questions[i];
let variables = question.variables;
let last_variable = getLastVariable(variables);
let new_question_name = preventDuplicateQuestionName(question.dataFile, output_language.replace(/'/g, "") + " translation of " + question.name);
let temp_var_name = randomVariableName(16); // temporary name, random to (almost) guarantee uniqueness
let controls = {formInputs: variables.map(v => v.guid).join(';')};
if (source_language === "'Specify with variable'")
controls['formSourceLanguageVariable'] = language_var_guid;
// Run expression to create R question
let new_r_question;
try {
new_r_question = data_file.newRQuestion(r_code, new_question_name, temp_var_name, last_variable, js_code, controls);
if (variables.length === 1)
new_r_question.questionType = "Text";
else
new_r_question.questionType = "Text - Multi";
insertAtHoverButtonIfShown(new_r_question);
}
catch (e) {
log("The translation could not be performed for this " + structure_name + " : " + e);
return false;
}
// Replace temporary variable names
nameSequentialVariables(new_r_question.variables, "translated");
new_r_questions.push(new_r_question);
}
reportNewRQuestion(new_r_questions, "Translated question(s)");
return true;
}