Sample code: Translate natural language with the Watson Language Translator service
Interactive code: When you see Run at the top of a code sample, you can run the code, check the results, make changes, and run it again. When you see Deploy, you can deploy the code into the developerWorks IBM Cloud account. To deploy the code, please sign in.
About this code
This code shows you how to use the node.js API for the Watson Language Translator service. Given some text, a source language, and a target language, Watson translates that text and returns one or more translations to you.
The code has five basic sections:
main()method. This is the entry point to the code. It creates the
LanguageTranslationV2object and then invokes the three translation functions.
englishToFrench()function. This takes text and a
LanguageTranslationV2object as its inputs and calls the Language Translator service to convert the input from English into French.
frenchToSpanish()function. This converts French text into Spanish.
spanishToEnglish()function converts Spanish to English.
- A set of default parameters and the code that runs this module if it
is invoked by itself (as opposed to being invoked by a
require()statement). See the node.js documentation for more information on the implications of the
require.main === modulestatement.
Sign in to run, change, and deploy this code.
This code takes a string as input and translates the string from English
to French, from French to Spanish, and finally from Spanish into English
again. The calls to the Watson Language Translator service are wrapped in
Promises to handle the asynchronous nature of the
Try changing the
textToTranslate parameter or the language
codes to see what happens.
Using this code once deployed
To use this code once you've deployed it, you must create an instance of the Watson Language Translator service:
To invoke the code, pass a JSON structure that matches the
defaultParameters object as shown above with the following
urlfields must be filled in with the credentials for your Language Translator service instance.
use_unauthenticatedfield must be
For more information, see the developerWorks tutorial Invoking actions on the IBM Cloud Functions platform.
- An overview of the Watson Language Translator service
- Demo the Watson Language Translator service
- How to use IBM App Connect with Watson Language Translator
- Create a Watson Powered Language Translator Slackbot
- Node.js journeys on developer.ibm.com/code
- Create a free Bluemix account