Categories We Write About

Create multi-language translation scripts

Here’s a set of multi-language translation scripts using different tools and technologies that can be integrated into websites, applications, or used in backend systems. Each script allows translation between multiple languages. Examples below are provided in JavaScript (frontend), Python (backend), and HTML (integrated UI) with APIs like Google Translate and LibreTranslate.


🌐 JavaScript (Browser) + Google Translate API

javascript
async function translateText(text, sourceLang, targetLang) { const apiKey = 'YOUR_GOOGLE_API_KEY'; const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`; const response = await fetch(url, { method: 'POST', body: JSON.stringify({ q: text, source: sourceLang, target: targetLang, format: 'text' }), headers: { 'Content-Type': 'application/json' } }); const data = await response.json(); return data.data.translations[0].translatedText; } // Example Usage translateText('Hello world', 'en', 'fr').then(console.log); // Bonjour le monde

🐍 Python (Backend) using requests + LibreTranslate

python
import requests def translate_text(text, source_lang, target_lang): url = 'https://libretranslate.de/translate' payload = { 'q': text, 'source': source_lang, 'target': target_lang, 'format': 'text' } headers = {'Content-Type': 'application/json'} response = requests.post(url, json=payload, headers=headers) return response.json()['translatedText'] # Example Usage print(translate_text("Good morning", "en", "es")) # Buenos días

🖥 HTML + JavaScript (UI with translation input/output)

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Multi-Language Translator</title> </head> <body> <h2>Translate Text</h2> <textarea id="inputText" placeholder="Enter text"></textarea><br> <select id="sourceLang"> <option value="en">English</option> <option value="es">Spanish</option> <option value="fr">French</option> </select> <select id="targetLang"> <option value="fr">French</option> <option value="en">English</option> <option value="es">Spanish</option> </select> <button onclick="translate()">Translate</button> <p><strong>Translated:</strong> <span id="output"></span></p> <script> async function translate() { const text = document.getElementById("inputText").value; const source = document.getElementById("sourceLang").value; const target = document.getElementById("targetLang").value; const response = await fetch('https://libretranslate.de/translate', { method: 'POST', body: JSON.stringify({ q: text, source: source, target: target, format: 'text' }), headers: { 'Content-Type': 'application/json' } }); const data = await response.json(); document.getElementById("output").innerText = data.translatedText; } </script> </body> </html>

🌍 Node.js Script (Server-Side) with @vitalets/google-translate-api

bash
npm install @vitalets/google-translate-api
javascript
const translate = require('@vitalets/google-translate-api'); translate('How are you?', { from: 'en', to: 'de' }).then(res => { console.log(res.text); // Wie geht es dir? }).catch(err => { console.error(err); });

🧩 PHP Translation with Google Translate API

php
<?php function translate($text, $source, $target, $apiKey) { $url = "https://translation.googleapis.com/language/translate/v2?key=$apiKey"; $fields = array( 'q' => $text, 'source' => $source, 'target' => $target, 'format' => 'text' ); $headers = array('Content-Type: application/json'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); $response = json_decode($result, true); return $response['data']['translations'][0]['translatedText']; } // Example usage $translated = translate("Thank you", "en", "it", "YOUR_GOOGLE_API_KEY"); echo $translated; ?>

🧠 Multi-language Support Idea with JSON Dictionary (Offline/Custom)

json
// translations.json { "hello": { "en": "Hello", "fr": "Bonjour", "es": "Hola", "de": "Hallo" }, "bye": { "en": "Goodbye", "fr": "Au revoir", "es": "Adiós", "de": "Tschüss" } }
javascript
const translations = require('./translations.json'); function localTranslate(key, lang) { return translations[key] ? translations[key][lang] || "Translation not found" : "Key not found"; } console.log(localTranslate("hello", "fr")); // Bonjour

These scripts can be integrated and adapted depending on your infrastructure (client or server-side) and your preferred APIs or libraries. Let me know if you need integration for a specific framework like React, Vue, Django, Laravel, or others.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About