xxxxxxxxxx
const sourceText = 'Hello, world!';
const targetLanguage = 'fr';
const translation = new Intl
.DisplayNames([targetLanguage], {type: 'language'})
.of(targetLanguage);
console.log(`Translation (${targetLanguage}): ${translation} => ${sourceText}`);
xxxxxxxxxx
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Language Translator</title>
</head>
<body>
<h1>Language Translation</h1>
<select id="languages">
<option value="en">English</option>
<option value="fr">French</option>
<option value="es">Spanish</option>
<!-- Add more language options as desired -->
</select>
<div id="content">
<p>This is some sample text that will be translated.</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
// Function to load Google Translate API
function googleTranslateElementInit() {
new google.translate.TranslateElement({ pageLanguage: 'en', includedLanguages: 'en,fr,es', layout: google.translate.TranslateElement.InlineLayout.SIMPLE }, 'google_translate_element');
}
// Load Google Translate API dynamically
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = 'https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'google-jssdk'));
// Change language when select option changes
$(document).ready(function() {
$('#languages').on('change', function() {
var language = $(this).val();
googleTranslateElementInit();
google.translate.TranslateElement({ pageLanguage: 'en', includedLanguages: language }, 'google_translate_element');
});
});
</script>
<div id="google_translate_element"></div>
</body>
</html>
xxxxxxxxxx
//You can use an api, adapting it to what you exactly need. As example is rapidApi,
//which is free to use.
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://nlp-translation.p.rapidapi.com/v1/translate',
params: {text: 'Hello, world!!', to: 'es', from: 'en'},
headers: {
'x-rapidapi-host': 'nlp-translation.p.rapidapi.com',
'x-rapidapi-key': 'c31cfcc8d1msh640834c65d07074p1022b9jsn451d8d5fa97c'
}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
xxxxxxxxxx
function Dancing(this_sprite) {
moveForward(this_sprite, 5);
if (isTouchingEdges(this_sprite)) {
edgesDisplace(this_sprite);
changePropBy(this_sprite, "direction", 180);
}
}
function math_random_int(a, b) {
if (a > b) {
// Swap a and b to ensure a is smaller.
var c = a;
a = b;
b = c;
}