JavaScript: Detect Text Language With Google Translate Api (Node.js)
May 13, 2021Google 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 string
async 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)
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn