<body>
<label>Choose a Lang:</label>
<select id="baselang">
<option value="ko">한국인</option>
<option value="en">English</option>
</select>
<br>
<input width="150px" id="text">
<button onclick="T()">Translate/번역하다</button>
<p id="result"></p>
<script>
async function T() {
// Vars
const resultHTML = document.getElementById("result");
const BaseLang = document.getElementById("baselang").value;
let TranLang = "ko"; // Use let instead of const
const text = document.getElementById("text").value;
const url = 'https://google-translate1.p.rapidapi.com/language/translate/v2';
// Figuring out the language to translate into
if (BaseLang === "en") { // Use === for comparison
TranLang = "ko"; // Use = for assignment
} else {
TranLang = "en";
}
const options = {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Accept-Encoding': 'application/gzip',
'X-RapidAPI-Key': '8222bd61f1mshad1ad1dfca1b2bcp1aa093jsnd4c03fb4a7ae',
'X-RapidAPI-Host': 'google-translate1.p.rapidapi.com'
},
body: new URLSearchParams({
q: text,
target: TranLang,
source: BaseLang
}).toString()
};
try {
const response = await fetch(url, options);
const result = await response.json();
console.log(result);
resultHTML.innerHTML = result.data.translations[0].translatedText;
} catch (error) {
console.error(error);
resultHTML.innerHTML = "An error occurred";
}
}
</script>
</body>