In this article I’ll explain how to create your automatic translator using Python and IBM Watson™ Language Translator.
A couple of weeks ago I was looking for an online service for automatic translations, preferably with a free quota. As result, I created a small automatic translator using Python and IBM Watson™ Language Translator.
With a Lite subscription you can get a free quota:
1,000,000 characters per month at no cost and includes the default translation models. When you upgrade to a paid plan, you can create custom models.
However, you can choose other plans, check the pricing plan page.
At this point you should be able to get your credentials. In the past IBM provided username and password while nowadays you get your apikey and your url.
And now, Python. We are going to use a client library to quickly get started with the various Watson APIs services. Simple installation with pip:
pip3 install --upgrade "ibm-watson>=4.2.1"
I used pip3, depending on the installed version on your machine, your command could be:
pip install --upgrade "ibm-watson>=4.2.1"
Now, create a new Python file inside your project folder, copy and paste the code below, check the comments.
import json from ibm_watson import LanguageTranslatorV3 from ibm_cloud_sdk_core.authenticators import IAMAuthenticator # Set some variables api_key = '<your-apikey>' api_url = '<your-url>' model_id = 'en-it' text_to_translate = 'Your content you want translate here' # Prepare the Authenticator authenticator = IAMAuthenticator(api_key) language_translator = LanguageTranslatorV3( version='2018-05-01', authenticator=authenticator ) language_translator.set_service_url(api_url ) # Translate translation = language_translator.translate( text=text_to_translate, model_id=model_id).get_result() # Print results print(json.dumps(translation, indent=2, ensure_ascii=False))
Change <your-apikey> and <your-url> with your personal credentials, run the software and get your results.
Here the documentation: https://cloud.ibm.com/apidocs/language-translator/language-translator?code=python#translate
In conclusion, it’s very easy to get a new IBM account and start practicing with automatic translations. IBM provide Watson™ Language Translator, or choose among different services, such as:
Tip: In addition, to play with IBM Watson™ Language Translator I dockerized my application using the official image, pull it from the Docker Hub.