Translate Text

Quickly translate text into dozens of languages using Cosmos.

About Translate Text

The Translate Text service works in a context-based processing, providing quick and precise translations for text.

Supported Languages

The output_language parameter allows to select the language for the translation, and it uses ISO 639 langugage codes. Here you can find some of the codes for some languages.

It is also allowed to specify the region (according to ISO 39 codes), for example for French it may be fr-FR, fr-BE, fr-CA, for English en-GB or en-US, or for Spanish es-ES, es-MX ...

ISO 639 CodeLanguage (English)Language (Original)
afAfrikaansAfrikaans
sqAlbanianShqip
amAmharicአማርኛ
arArabicالعربية
hyArmenianՀայերեն
azAzerbaijaniAzərbaycan dili
euBasqueEuskara
beBelarusianБеларуская
bnBengaliবাংলা
bsBosnianBosanski
bgBulgarianБългарски
caCatalanCatalà
zhChinese中文
hrCroatianHrvatski
csCzechČeština
daDanishDansk
nlDutchNederlands
enEnglishEnglish
etEstonianEesti
fiFinnishSuomi
frFrenchFrançais
kaGeorgianქართული
deGermanDeutsch
elGreekΕλληνικά
guGujaratiગુજરાતી
heHebrewעברית
hiHindiहिन्दी
huHungarianMagyar
isIcelandicÍslenska
idIndonesianBahasa Indonesia
itItalianItaliano
jaJapanese日本語
knKannadaಕನ್ನಡ
kkKazakhҚазақша
koKorean한국어
lvLatvianLatviešu
ltLithuanianLietuvių
mkMacedonianМакедонски
msMalayBahasa Melayu
mlMalayalamമലയാളം
mrMarathiमराठी
mnMongolianМонгол
noNorwegianNorsk
faPersianفارسی
plPolishPolski
ptPortuguesePortuguês
paPunjabiਪੰਜਾਬੀ
roRomanianRomână
ruRussianРусский
srSerbianСрпски
skSlovakSlovenčina
slSlovenianSlovenščina
esSpanishEspañol
svSwedishSvenska
taTamilதமிழ்
teTeluguతెలుగు
thThaiไทย
trTurkishTürkçe
ukUkrainianУкраїнська
urUrduاردو
viVietnameseTiếng Việt
cyWelshCymraeg
yoYorubaYorùbá

Refer to the ISO 639-1 codes for more options.


Step-by-step Tutorial

1. Prerequisites

Before you begin, ensure you have:

2. Setup Cosmos Python Client

Using Python Cosmos client you can perform the API requests in a convenient way.

2.1. Install Cosmos Python Client:

In order to install the Python client, you can add it to your project by doing:

poetry add delos-cosmos

Or install it in your virtual environment with:

pip install delos-cosmos

2.2. Authenticate Requests:

  • Initialize the client with your API key:
from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey=your-cosmos-api-key)

2.3. Call API:

  • You can start invoking any Cosmos endpoints. For example, let's try the /health endpoint to check the validity of your API key and the availability of the client services:
response = cosmos_client.status_health_request()
print(response)

3. Translate text

Initialize the client and perform your request:

from cosmos import CosmosClient

cosmos_client = CosmosClient(apikey="your-cosmos-api-key")
response = cosmos_client.translate_text_request(text="Hello, how are you?", output_language="es")
print(response)

The parameters for the text translation request are:

ParameterDescriptionExample
textThe text to translate"path/to/file.docx"
output_languageThe ISO 639-1 code of the target languagees-ES for Spanish
input_language (optional)The ISO 639-1 code of the original languagefr-FR for French

If your request is successful, you will receive a response containing the translated text. Example:

{
  "request_id": "f9c3e66-0a8b-4b62-a292-2c9371b3f020",
  "status_code": 200,
  "translation": "Hola, ¿cómo estás?"
}

4. Handle Errors

  • If there’s an error, you’ll receive a response with details. For example, if the text is missing:
{
  "status_code": 400,
  "error_message": "No text provided for translation."
}