Language Translation
If you want to offer translation on-the-fly, whether it's as short as a chat message or as long as a news article, and deliver it to any number of subscribers, in realtime, PubNub Functions lets you do it. Select from a list of pre-built Functions, or build an integration with a service of your choice.
Here are a few suggestions:
- IBM Watson Language Translator provides an API for translating and publishing text for 11 different languages.
- Amazon Translate removes the complexity of building translation capabilities into your applications with a simple API call. This makes it easy to localize an application, or process multilingual data within your existing workflows.
- Microsoft Translator is a cognitive services offering from the Microsoft Cloud and Enterprise division. It provides a scalable, compliant, and customizable neural machine translation cloud service that supports over 60 languages.
IBM Watson Language Translator Tutorial
The IBM Watson Language Translator provides an API for translating and publishing text in 11 different languages.
IBM Watson Function Code
// Require the console module
const console = require('console');
// Example languages to translate to: English, Spanish, French
// ['en', 'es', 'fr']
// Require the xhr module. We'll be using a third-party REST endpoint for
// translation. We need to make a GET request to the endpoint with
// parameters and we will get back the translated text in the response.
// The xhr module helps in sending out HTTP requests.
const xhr = require('xhr');
const query = require('codec/query_string');
const base64 = require('codec/base64');
// Store your API key securely in the vault using the MY SECRETS button.
// The vault get param for your IBM key should be `ibm_translate_api_key`
const vault = require('vault');
export default (request) => {
//API key
return vault.get('ibm_translate_api_key').then((apiKey) => {
// translation API URL
const apiUrl = 'https://api.us-east.language-translator.watson.cloud.ibm.com/language-translator/api/v3/translate?version=2018-05-01';
// extract from request, channel on which the message was published
const channel = request.channels[0];
// language to translate to
const outputLang = 'es'; // spanish
// Set the body's `model_id` to `input-output` format with 2 letter
// language codes. Example: English to Spanish would be 'en-es'
const body = {
model_id: request.message.input_lang + '-' + outputLang,
text: [request.message.text]
};
const httpOptions = {
method: 'POST',
headers: {
'Authorization': 'Basic ' + base64.btoa('apikey:' + apiKey),
'Content-Type': 'application/json'
},
body
};
// Invoke xhr fetch to get the HTTP response
return xhr.fetch(apiUrl, httpOptions)
.then(r => {
const body = JSON.parse(r.body);
request.message.original_text = request.message.text;
request.message.output_lang = outputLang;
request.message.text = body.translations[0].translation;
return request.ok();
})
.catch((e) => {
console.error(e);
return request.abort();
});
});
};
Getting started with the Watson APIs
First, you’ll need to get started with a Bluemix account to take advantage of the Watson APIs.
Sign up with the Bluemix signup form.
Open the IBM Watson Language Translator API page, and add it to your service dashboard.
On the service credentials pane of your new service instance, make a note of the username and password.
Setting up the PubNub Function
Next, set up a PubNub Function to get your code running on the network.
Create a new function. Go to your Admin Portal and create a new module, and then create a new Before Publish function. The function should be set up to trigger on a specific set of channels (such as
chat.*
) or on all channels using wildcards (*
).Update the
username
andpassword
constants with your Watson credentials from the last Watson setup step above. Update thelangs
array with the languages you want to use, and click the red Save button.Click Start module to start the function, and test it using the Test Payload field and Publish button on the left.
Tip
For a more detailed overview of writing a function, refer to these instructions on the Functions Overview page.
Code Walkthrough
This function enables communication between multiple participants in their native languages. It supports English, French, and Spanish, but you can support additional languages by adding them to the langs
array.
A message is sent to the input channel. Then it's translated using the IBM Watson API while the message is in transit. The published message is appended with a translation before it reaches subscribers.
Input: Publish a message text on the input channel.
{
"text": "Hi",
"input_lang": "en"
}
Output (Spanish): The text is translated and added to the message payload.
{
"text": "Hola",
"input_lang": "en",
"original_text": "Hi",
"output_lang": "es"
}