Language Translation has become an essential requirement for many businesses as their products or applications are used by a diverse number of people. On top of that, businesses make use of these translated texts for other Natural Language Processing (NLP) tools such as sentiment analysis, advertising, and virtual assistants. Consequently, developers often face the challenge of developing websites that support text translation between multiple languages.
Fortunately, there are some APIs that can be used to overcome this challenge. NLP Translation API is one such them that provides the capability to translate text into another language instantly.
What is NLP Translation API?
NLP Translation API is a simple and easy-to-use API that delivers instant language translation using high-quality Neural Machine Translation (NMT). Using this API, you can translate words and sentences into more than 110 target languages. It can produce more relevant translations than other APIs thanks to its ‘whole sentences translation at a time’ method.
This API enables you to specify the text to translate in HTML, text, or JSON format and also specify more than one target language in a single request. Moreover, its ‘protected words’ feature lets you avoid translating specific words of your choice.
When considering the cost of the API, it offers reasonable pricing plans with a free subscription. The support for various programming languages such as Python, PHP, Ruby, and Javascript is a great plus of this API which helps to integrate and use it with many applications. In this article, we will see how to use it with multiple programming languages.
How does the NLP Translation API work?
NLP Translation API works according to a simple API logic. It sends a request to a specific endpoint and obtains the necessary output as the response. When sending a request, it includes the API key and host as authentication parameters to let the server identify it as a valid request. The body of the API request contains the required parameters that define the text to translate, its source, and target languages. Once the API server has received and processed the request, it will send back the translated text to the client in JSON format.
Target Audience for the NLP Translation API
Business Entities
Language translation capability enables businesses to provide multilingual support to their customers in their applications. It makes communication between cross cultures easier while also delivering a positive user experience. Businesses can easily integrate this API into their internal systems like help desk, virtual assistant applications, and emails for this purpose.
Translation Service Providers
NLP Translation API can translate text into multiple languages in a single request. It comes with affordable pricing plans which support up to 500,000,000 characters per month, while any additional characters will cost only a minimal amount. Therefore this API is highly beneficial for companies that provide online professional translation services to worldwide customers. They can translate texts into various languages faster at a very low cost by integrating NLP Translation API for their translation applications.
Data Analytics Providers
Many business entities use data analytics to identify trends and usage patterns of their products as it helps to retrieve important insights related to the business. However, this data can be in multiple languages and needs to be translated into one language before processing. This provides an opportunity for data analytics companies to make use of this API as a part of their analytics applications at a lower cost.
Learners
NLP Translation API is easy to understand as well as integrate into an application. Its free subscription offers sufficient features for anyone to test and learn. If you are a student, you can use it to learn other languages without having to identify the source language. Due to the simplicity of this API, learners can develop their own simple applications and integrate it for language translation and understanding.
Educators
Nowadays, a lot of educators are moving to online teaching methods providing courses through e-learning websites. Most of these courses are offered in the English language. However, there can be students who have difficulties in understanding English. In such a case, online course content creators can use this API to translate course modules into other languages. This will, in turn, help them reach more international audiences.
How to connect to the NLP Translation API Tutorial – Step by Step
Step 1 – Sign up and Get a RapidAPI Account.
RapidAPI is the World’s largest API marketplace used by more than a million developers worldwide. You can use it to search and connect to thousands of APIs using a single SDK, API key, and Dashboard.
To create a RapidAPI account, visit rapidapi.com and click on the Sign Up icon. You can use your Google, Github, or Facebook account for Single Sign-on (SSO) or create an account manually.
Step 2 – Search the API Marketplace
Navigate to the Marketplace and search for “NLP Translation APIs.” Then, select the ‘NLP Translation’ API from the search results.
Step 3 – Subscribe to the API
You need to subscribe to this API to use it. Navigate to the “pricing” section of the API and subscribe to your preferred plan. For the purpose of this article, we select the free basic plan of the NLP Translation API. Then, simply click on the “subscribe” button to activate the subscription.
Step 4 – Test the API Subscription
After subscribing to the API, run a test to check the endpoints. For that, navigate to the ‘Endpoints’ section of the API page which shows all the available endpoints.
Next, select the endpoint you want to test and add the required values for the parameters. In this instance, we will select the ‘JSON Data Translate endpoint. Each API endpoint includes code snippets in multiple languages and automatically adds the API keys.
Finally, click on the “Test Endpoint” Button to test the API. You will get a response with a 200 status code upon successful testing of the API endpoint. On the other hand, it will output an error code and a message if there is an error in the request.
NLP Translation API Endpoints
Currently, NLP Translation API consists of two endpoints, as shown in the following image.
JSON Data Translate
This API endpoint is used to translate a text inside a JSON formatted string with ‘protected keys’ and ‘protected words’ parameters. It is accessible using the POST method. In the ‘protected_words’ parameter, you can define the words in the text that you do not want to be translated. You can define them as direct words or as JSON keys in the ‘protected_keys’ parameter. Make sure to use a semicolon as the separator between the words. You only have to specify the following parameters in the request body as described in the About page of the API.
- protected_words
- from
- to
- protected_keys
- json
The source and target languages are defined in ‘from’ and ‘to’ parameters, respectively, and they need to be specified as language code. You can find the list of language codes on its About page.
Translate
This endpoint is used to translate text or HTML. You can access it with both GET and POST methods. There, you only have to specify the text or HTML to translate and the target language of ‘text’ and ‘to’ parameters, respectively. Optionally, you can include the source language and the words that need to be omitted in translation as protected keys. However, it is recommended to include the ‘from’ parameter for faster processing.
A successful response for a request to these endpoints will look something like the following.
Integrating the NLP Translation API to an Application
In this section, we will see how to integrate the NLP Translation API into a software application using various programming languages such as Python, PHP, Ruby, and Javascript with example code snippets. To execute these code snippets, first, you need to install the specific programming language on your computer. We will be using the “JSON Data Translate” endpoint for all the code snippets.
You can simply integrate the following code snippets into your application based on its programming language. You need to include the ‘X-RapidAPI-key and X-RapidAPI-host parameters in the header of each code.
Python Code Snippet (requests)
To integrate the API with Python, first, you have to install the ‘requests’ module and import it into the application. In the following code, the payload has been defined in an encoded format which you can also define in decoded JSON format. Instead of using ‘requests. request’ to send the API call, you can also use ‘requests. post’ method. Other than this HTTP module, code snippets are provided in HTTP. client and Unirest as well.
import requests url = "https://nlp-translation.p.rapidapi.com/v1/jsontranslate" payload = "from=en&to=es&json=%7B%22name%22%3A%7B%22firstName%22%3A%22George%22%2C%22lastName%22%3A%22Bush%22%7D%2C%22age%22%3A30%2C%22Address%22%3Anull%2C%22cities%22%3A%5B%7B%22code%22%3A%22NYC%22%2C%22name%22%3A%22New%20York%20City%22%2C%22state%22%3A%22New%20York%22%2C%22lat%22%3A40.73061%2C%22long%22%3A-73.935242%7D%2C%7B%22code%22%3A%22LA%22%2C%22name%22%3A%22City%20of%20Los%20Angeles%22%2C%22state%22%3A%22California%22%2C%22lat%22%3A34.05%2C%22long%22%3A-118.25%7D%5D%7D&protected_keys=name.firstName%3Bname.lastName%3Bcities.code&protected_words=New%20York%3BLos%20Angeles" headers = { 'content-type': "application/x-www-form-urlencoded", 'x-rapidapi-key': "cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5", 'x-rapidapi-host': "nlp-translation.p.rapidapi.com" } response = requests.request("POST", url, data=payload, headers=headers) print(response.text)
PHP Code Snippet (HTTP v2)
When it comes to integrating the API using PHP, first, create the client, request, and body class objects. Then append the parameters into the body. Next, you need to set the API URL, method, body, and headers separately to the request object. Finally, send the request through the send method of the client object. Here, you can also use cURL, HTTP V1, and Unirest/Request code snippets provided on the API website.
<?php $client = new http\Client; $request = new http\Client\Request; $body = new http\Message\Body; $body->append(new http\QueryString([ 'from' => 'en', 'to' => 'es', 'json' => '{"name":{ "firstName":"George", "lastName":"Bush" }, "age":30, "Address":null, "cities":[{ "code":"NYC", "name":"New York City", "state":"New York", "lat":40.73061, "long":-73.935242 },{ "code":"LA", "name":"City of Los Angeles", "state":"California", "lat":34.05, "long":-118.25 }] }', 'protected_keys' => 'name.firstName;name.lastName;cities.code', 'protected_words' => 'New York;Los Angeles' ])); $request->setRequestUrl('https://nlp-translation.p.rapidapi.com/v1/jsontranslate'); $request->setRequestMethod('POST'); $request->setBody($body); $request->setHeaders([ 'content-type' => 'application/x-www-form-urlencoded', 'x-rapidapi-key' => 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host' => 'nlp-translation.p.rapidapi.com' ]); $client->enqueue($request)->send(); $response = $client->getResponse(); echo $response->getBody();
Ruby (net::http)
If you are using the net::http module of Ruby, import OpenSSL, and URI modules at the beginning. In this instance, bypass SSL certification verification by setting it to ‘VERIFY_NONE’. Then set the required parameters for the API as in the PHP example and send the request using http.request method.
require 'uri' require 'net/http' require 'openssl' url = URI("https://nlp-translation.p.rapidapi.com/v1/jsontranslate") http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Post.new(url) request["content-type"] = 'application/x-www-form-urlencoded' request["x-rapidapi-key"] = 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5' request["x-rapidapi-host"] = 'nlp-translation.p.rapidapi.com' request.body = "from=en&to=es&json=%7B%22name%22%3A%7B%22firstName%22%3A%22George%22%2C%22lastName%22%3A%22Bush%22%7D%2C%22age%22%3A30%2C%22Address%22%3Anull%2C%22cities%22%3A%5B%7B%22code%22%3A%22NYC%22%2C%22name%22%3A%22New%20York%20City%22%2C%22state%22%3A%22New%20York%22%2C%22lat%22%3A40.73061%2C%22long%22%3A-73.935242%7D%2C%7B%22code%22%3A%22LA%22%2C%22name%22%3A%22City%20of%20Los%20Angeles%22%2C%22state%22%3A%22California%22%2C%22lat%22%3A34.05%2C%22long%22%3A-118.25%7D%5D%7D&protected_keys=name.firstName%3Bname.lastName%3Bcities.code&protected_words=New%20York%3BLos%20Angeles" response = http.request(request) puts response.read_body
Javascript (Axios)
Make sure to install the Axios package when using Javascript Axios to integrate this API.
import axios from "axios"; const options = { method: 'POST', url: 'https://nlp-translation.p.rapidapi.com/v1/jsontranslate', headers: { 'content-type': 'application/x-www-form-urlencoded', 'x-rapidapi-key': 'cJvLRNK0GfdM9WSMbQe3inU7REn8JVy5', 'x-rapidapi-host': 'nlp-translation.p.rapidapi.com' }, data: { from: 'en', to: 'es', json: '{"name":{"firstName":"George","lastName":"Bush"},"age":30,"Address":null,"cities":[{"code":"NYC","name":"New York City","state":"New York","lat":40.73061,"long":-73.935242},{"code":"LA","name":"City of Los Angeles","state":"California","lat":34.05,"long":-118.25}]}', protected_keys: 'name.firstName;name.lastName;cities.code', protected_words: 'New York;Los Angeles' } }; axios.request(options).then(function (response) { console.log(response.data); }).catch(function (error) { console.error(error); });
Alternatives to the NLP Translation API
In addition to the NLP translation API, there are also some other language translation APIs available at RapidAPI. The following is a list of such APIs.
Google Translate – A highly responsive translation API that can be used for fast and dynamic language translation.
Microsoft Translator Text – A service from Microsoft Azure that enables real-time text translation, language detention, dictionary lookup, and transliteration.
Kiara Translate V2 – This simple API takes input text in JSON format and translates it into the target language
SYSTRAN.io Translation and NLP API – This API provides a list of endpoints for text translation, Multilingual Dictionary lookups, Natural Language Processing, and Text Extraction
AI Compare Translation API – This API allows you to use many translation APIs from different providers like Google Cloud, AWS, and IBM with a single API token.
Benefits of the NLP Translation API
Saves Time and Cost
This API helps to save your time and cost significantly as a manual translation by a professional translator is usually a costly and time-consuming process. It offers affordable pricing plans to suit your needs. These plans are a lot cheaper than manual translation and provide a faster translation. Moreover, this API can support unlimited requests at a rate of $0.00001 for any additional character which exceeds the amount of characters covered by the plan.
Easy to Integrate
NLP Translation API lets you translate text with a single API call by defining only a few parameters. This eliminates the need for developing complex applications with multiple language translations. Besides, the API interface provides example code snippets in many programming languages so that even someone with little or no background with APIs can master the API quickly.
Extensive Language Coverage
NLP Translation API supports the translation of text into more than 110 languages which is a wider language coverage than most other language translation APIs. Furthermore, it can use words or sentences in different formats (JSON, text, and HTML ) and translate them into multiple languages using a single request.
Customizations
This API allows users to customize their search using several parameters. For instance, protected keys and words parameters let you define what words should be omitted when translating. Additionally, you can simply skip specifying the source language parameter if you do not know the language.
Summary
NLP Translation API is a quite straightforward and convenient language translation API that is used by many applications due to its great benefits. In this article, we learned about the available endpoints of this API, its benefits, and its target audience. We also provided a few sample code snippets in Python, PHP, Ruby, and Javascript to show how to integrate them into an application.
Leave a Reply