Google Cloud Translation Setup
- Enable Billing
- Creating service accounts and keys (not necessary if you are using google clouds function)
- Enable Cloud Translation API
Install
npm install --save @google-cloud/translate
Code
const {Translate} = require('@google-cloud/translate').v2;const translate = new Translate();// text could be string or array of stringasync function detectLanguageGoogle(text) { let [detections] = await translate.detect(text); // enable this following code to always return array // detections = Array.isArray(detections) ? detections : [detections]; return detections}
Test
const result = await detectLanguageGoogle('What do we know so far about the Covid-19 variants?')
{ "confidence": 1, "language": "en", "input": "What do we know so far about the Covid-19 variants?"},
Test 2
const values = [ 'What do we know so far about the Covid-19 variants?', // English "Que savons-nous jusqu'à présent des variantes du Covid-19?", // French "Was wissen wir bisher über die Covid-19-Varianten?", // German "¿Qué sabemos hasta ahora sobre las variantes de Covid-19?", // Spanish "Apa yang kita ketahui sejauh ini tentang varian Covid-19?", // Indonesian "到目前为止,我们对Covid-19变体了解多少?", // Chinese Simplified "到目前為止,我們對Covid-19變體了解多少?", // Chinese Traditional "Covid-19バリアントについてこれまでに何を知っていますか?", // Japanese "हम कोविड -19 वेरिएंट के बारे में अब तक क्या जानते हैं?", // Hindi "ما الذي نعرفه حتى الآن عن بدائل Covid-19؟", // Arabic "Что мы знаем о вариантах Covid-19?" // Russian]const results = await detectLanguageGoogle(values)
[ { "confidence": 1, "language": "en", "input": "What do we know so far about the Covid-19 variants?" }, { "language": "fr", "confidence": 1, "input": "Que savons-nous jusqu'à présent des variantes du Covid-19?" }, { "language": "de", "confidence": 1, "input": "Was wissen wir bisher über die Covid-19-Varianten?" }, { "language": "es", "confidence": 1, "input": "¿Qué sabemos hasta ahora sobre las variantes de Covid-19?" }, { "language": "id", "confidence": 0.8346776366233826, "input": "Apa yang kita ketahui sejauh ini tentang varian Covid-19?" }, { "confidence": 1, "language": "zh-CN", "input": "到目前为止,我们对Covid-19变体了解多少?" }, { "confidence": 1, "language": "zh-TW", "input": "到目前為止,我們對Covid-19變體了解多少?" }, { "language": "ja", "confidence": 1, "input": "Covid-19バリアントについてこれまでに何を知っていますか?" }, { "language": "hi", "confidence": 1, "input": "हम कोविड -19 वेरिएंट के बारे में अब तक क्या जानते हैं?" }, { "language": "ar", "confidence": 1, "input": "ما الذي نعرفه حتى الآن عن بدائل Covid-19؟" }, { "confidence": 1, "language": "ru", "input": "Что мы знаем о вариантах Covid-19?" }]
- First 500,000 characters* per month is free ($10 monthly credit)
- $20‡ per million characters (up to 1 billion characters)