Leveraging Google Translate API in Your Node.js Application
Written on
Introduction to Google Translate API
The google-translate-api library enables the integration of Google Translate capabilities into your server-side JavaScript applications. In this guide, we will explore how to effectively utilize this package within a Node.js environment.
Installation Process
To install the necessary package, simply execute the following command in your terminal:
npm install @vitalets/google-translate-api
Utilizing the API for Translations
You can utilize the translate function provided by this module for performing translations. Below is an example implementation:
const translate = require('@vitalets/google-translate-api');
(async () => {
try {
const res = await translate('je parle français', { to: 'en' });
console.log(res.text);
console.log(res.from.language.iso);
} catch (error) {
console.log(error);}
})();
The translate function returns a promise containing the translation result. The first parameter is the text you wish to translate, while the second parameter includes the language code for the target language. The res.text property contains the translated text, and res.from.language.iso provides the language code for the original text.
Handling Typos in Text
The Google Translate API is capable of processing text that contains typographical errors. For instance, you can input a phrase with a mistake as shown below:
const translate = require('@vitalets/google-translate-api');
(async () => {
try {
const res = await translate('I spea French', { from: 'en', to: 'fr' });
console.log(res.text);
console.log(res.from.text.autoCorrected);
console.log(res.from.text.value);
console.log(res.from.text.didYouMean);
} catch (error) {
console.log(error);}
})();
In this case, the API will identify the typo and return the corrected translation, along with additional details indicating whether corrections were made.
Adding Custom Languages
You can also expand the list of supported languages. For example:
const translate = require('@vitalets/google-translate-api');
translate.languages['sr-Latn'] = 'Serbian Latin';
(async () => {
try {
const res = await translate('I spea French', { to: 'sr-Latn' });
console.log(res.text);
} catch (error) {
console.log(error);}
})();
This code allows you to define a new language for translation, which can be utilized just like the built-in ones.
Making Requests through a Proxy
If you need to route your requests through a proxy, you can do so using the following approach:
const translate = require('@vitalets/google-translate-api');
const tunnel = require('tunnel');
(async () => {
try {
const res = await translate('I spea French', {
to: 'fr',
agent: tunnel.httpsOverHttp({
proxy: {
host: '138.68.60.8',
proxyAuth: 'user:pass',
port: '8080',
headers: {
'User-Agent': 'Node'}
}
})
});
console.log(res.text);
} catch (error) {
console.log(error);}
})();
In this example, the tunnel library is utilized to send requests over a proxy server. You need to specify the proxy options and credentials to successfully make the translation request.
Conclusion
By leveraging the google-translate-api module, you can seamlessly integrate Google Translate functionalities into your Node.js applications.
This video provides an overview of using the Google Cloud Translate API with Node.js, detailing its setup and practical applications.
In this video, you'll learn how to build a free Google Translate API using Node.js, presented in Arabic for easier understanding.