Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@iamtraction/google-translate
Advanced tools
A Node.JS library to consume Google Translate API for free.
A Node.JS library to consume Google Translate for free.
# Stable version, from npm repository
npm install --save @iamtraction/google-translate
# Latest version, from GitHub repository
npm install --save iamtraction/google-translate
// If you've installed from npm, do:
const translate = require('@iamtraction/google-translate');
// If you've installed from GitHub, do:
const translate = require('google-translate');
translate(text, options)
translate(text, options).then(console.log).catch(console.error);
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
text | String | No | - | The text you want to translate. |
options | Object | - | - | The options for translating. |
options.from | String | Yes | 'auto' | The language name/ISO 639-1 code to translate from. If none is given, it will auto detect the source language. |
options.to | String | Yes | 'en' | The language name/ISO 639-1 code to translate to. If none is given, it will translate to English. |
options.raw | Boolean | Yes | false | If true , it will return the raw output that was received from Google Translate. |
Promise<Object>
Response Object:
Key | Type | Description |
---|---|---|
text | String | The translated text. |
from | Object | - |
from.language | Object | - |
from.language.didYouMean | Boolean | Whether or not the API suggest a correction in the source language. |
from.language.iso | String | The ISO 639-1 code of the language that the API has recognized in the text. |
from.text | Object | - |
from.text.autoCorrected | Boolean | Whether or not the API has auto corrected the original text. |
from.text.value | String | The auto corrected text or the text with suggested corrections. Only returned if from.text.autoCorrected or from.text.didYouMean is true . |
from.text.didYouMean | Boolean | Wherether or not the API has suggested corrections to the text |
raw | String | The raw response from Google Translate servers. Only returned if options.raw is true in the request options. |
translate('Tu es incroyable!', { to: 'en' }).then(res => {
console.log(res.text); // OUTPUT: You are amazing!
}).catch(err => {
console.error(err);
});
translate('Thank you', { from: 'en', to: 'fr' }).then(res => {
console.log(res.text); // OUTPUT: Je vous remercie
console.log(res.from.autoCorrected); // OUTPUT: true
console.log(res.from.text.value); // OUTPUT: [Thank] you
console.log(res.from.text.didYouMean); // OUTPUT: false
}).catch(err => {
console.error(err);
});
translate('Thank you', { from: 'en', to: 'fr' }).then(res => {
console.log(res.text); // OUTPUT: ''
console.log(res.from.autoCorrected); // OUTPUT: false
console.log(res.from.text.value); // OUTPUT: [Thank] you
console.log(res.from.text.didYouMean); // OUTPUT: true
}).catch(err => {
console.error(err);
});
If you liked this project, please give it a ⭐ in GitHub.
Credits to matheuss for writing the original version of this library. I rewrote this, with improvements and without using many external libraries, as his library was not actively developed and had vulnerabilities.
FAQs
A Node.JS library to consume Google Translate API for free.
We found that @iamtraction/google-translate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.