Skip to main content

Translate Text

Run in Postman

In this article you will learn how to translate a text using APIs.

Prerequisites

Make sure to follow Getting Started to log in and install the NeuralSpace Translation Service. If you are using APIs, save your authorization token in a variable called AUTHORIZATION_TOKEN before moving ahead.

Translate

Pass your text in the text attribute and set sourceLanguage and targetLanguage using the language codes mentioned in Language Support

Translate API
curl --location --request POST 'https://platform.neuralspace.ai/api/translation/v1/translate' \
--header 'Accept: application/json, text/plain, */*' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json;charset=UTF-8' \
--data-raw '{
"text": "I work at Neuralspace",
"sourceLanguage":"en",
"targetLanguage": "hi"}'

Translate Text with Entities

When you translate a sentence with annotations, like entities, you loose the annotations in the translated text as the words get rearranged and it is hard to find which phrase in the translated text corresponds to which entity in the source language text.

This is exactly why we have this API. You can pass entity annotations to our translation API and preserve these annotations in the translated text too.

Pass your text in the text attribute and set sourceLanguage and targetLanguage using the language codes mentioned in Language Support. entities are the annotations you want to preserve in the translated text. Entities have to have a start and an end character index denoting where in the text they occur. Additionally, each entity should have a value attribute which should contain the exact entity value, and an entity attribute denoting the type of entity it is.

Translate with Entities API
curl --location --request POST 'https://platform.neuralspace.ai/api/translation/v1/annotated/translate' \
--header 'Accept: application/json, text/plain, */*' \
--header "authorization: ${AUTHORIZATION_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw '{
"text": "राजस्थान के करौली शहर में हिंदू नववर्ष के मौके पर हिंदुओं ने 2 अप्रैल को एक बाइक रैली निकाली थी।",
"sourceLanguage": "hi",
"targetLanguage": "en",
"entities": [
{
"start": 61,
"end": 62,
"value": "2",
"entity": "cardinal"
},
{
"start": 73,
"end": 75,
"value": "एक",
"entity": "cardinal"
},
{
"value": "राजस्थान",
"start": 0,
"end": 8,
"entity": "location"
},
{
"value": "करौली",
"start": 12,
"end": 17,
"entity": "location"
}
]
}'

The response object looks something like the following. Note that the entities are preserved in the translated output.

{
"success": true,
"message": "Data fetched succssfully",
"data": {
"text": "राजस्थान के करौली शहर में हिंदू नववर्ष के मौके पर हिंदुओं ने 2 अप्रैल को एक बाइक रैली निकाली थी।",
"translatedText": "On the occasion of Hindu New Year in Karauli city of Rajasthan Hindus took out a a bike rally on 2 April.",
"suggestions": [
"On the occasion of Hindu New Year in Karauli city of Rajasthan Hindus took out a a bike rally on 2 April."
],
"entities": [
{
"start": 37,
"end": 44,
"value": "Karauli",
"entity": "location"
},
{
"start": 53,
"end": 62,
"value": "Rajasthan",
"entity": "location"
},
{
"start": 10,
"end": 11,
"value": "a",
"entity": "cardinal"
},
{
"start": 97,
"end": 98,
"value": "2",
"entity": "cardinal"
}
]
}
}