多语言调查是指在一个调查问卷中支持多种语言,以便不同语言背景的用户都能理解和参与。这种调查通常用于国际化的应用程序或网站,以提高用户体验和数据的多样性。
可以使用JavaScript和HTML来实现多语言支持。以下是一个简单的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Multi-language Survey</title>
<script>
const translations = {
en: {
title: "Survey",
question1: "What is your favorite color?",
submit: "Submit"
},
zh: {
title: "调查",
question1: "你最喜欢的颜色是什么?",
submit: "提交"
}
};
function setLanguage(lang) {
document.getElementById('title').innerText = translations[lang].title;
document.getElementById('question1').innerText = translations[lang].question1;
document.getElementById('submit').innerText = translations[lang].submit;
}
</script>
</head>
<body>
<h1 id="title"></h1>
<p id="question1"></p>
<button id="submit">Submit</button>
<select onchange="setLanguage(this.value)">
<option value="en">English</option>
<option value="zh">中文</option>
</select>
</body>
</html>
在后端,可以使用多种编程语言和框架来实现多语言支持。以下是一个使用Node.js和Express的简单示例:
const express = require('express');
const app = express();
const port = 3000;
const translations = {
en: {
title: "Survey",
question1: "What is your favorite color?"
},
zh: {
title: "调查",
question1: "你最喜欢的颜色是什么?"
}
};
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
const lang = req.query.lang || 'en';
res.send(`
<!DOCTYPE html>
<html lang="${lang}">
<head>
<meta charset="UTF-8">
<title>${translations[lang].title}</title>
</head>
<body>
<h1>${translations[lang].title}</h1>
<p>${translations[lang].question1}</p>
<form action="/submit" method="post">
<input type="hidden" name="lang" value="${lang}">
<input type="text" name="answer">
<button type="submit">Submit</button>
</form>
</body>
</html>
`);
});
app.post('/submit', (req, res) => {
const { lang, answer } = req.body;
console.log(`Language: ${lang}, Answer: ${answer}`);
res.send('Thank you for your submission!');
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
通过以上方法和示例代码,你可以创建一个支持多语言的调查问卷,提升用户体验和数据多样性。
领取专属 10元无门槛券
手把手带您无忧上云