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 Code | Language (English) | Language (Original) |
---|---|---|
af | Afrikaans | Afrikaans |
sq | Albanian | Shqip |
am | Amharic | አማርኛ |
ar | Arabic | العربية |
hy | Armenian | Հայերեն |
az | Azerbaijani | Azərbaycan dili |
eu | Basque | Euskara |
be | Belarusian | Беларуская |
bn | Bengali | বাংলা |
bs | Bosnian | Bosanski |
bg | Bulgarian | Български |
ca | Catalan | Català |
zh | Chinese | 中文 |
hr | Croatian | Hrvatski |
cs | Czech | Čeština |
da | Danish | Dansk |
nl | Dutch | Nederlands |
en | English | English |
et | Estonian | Eesti |
fi | Finnish | Suomi |
fr | French | Français |
ka | Georgian | ქართული |
de | German | Deutsch |
el | Greek | Ελληνικά |
gu | Gujarati | ગુજરાતી |
he | Hebrew | עברית |
hi | Hindi | हिन्दी |
hu | Hungarian | Magyar |
is | Icelandic | Íslenska |
id | Indonesian | Bahasa Indonesia |
it | Italian | Italiano |
ja | Japanese | 日本語 |
kn | Kannada | ಕನ್ನಡ |
kk | Kazakh | Қазақша |
ko | Korean | 한국어 |
lv | Latvian | Latviešu |
lt | Lithuanian | Lietuvių |
mk | Macedonian | Македонски |
ms | Malay | Bahasa Melayu |
ml | Malayalam | മലയാളം |
mr | Marathi | मराठी |
mn | Mongolian | Монгол |
no | Norwegian | Norsk |
fa | Persian | فارسی |
pl | Polish | Polski |
pt | Portuguese | Português |
pa | Punjabi | ਪੰਜਾਬੀ |
ro | Romanian | Română |
ru | Russian | Русский |
sr | Serbian | Српски |
sk | Slovak | Slovenčina |
sl | Slovenian | Slovenščina |
es | Spanish | Español |
sv | Swedish | Svenska |
ta | Tamil | தமிழ் |
te | Telugu | తెలుగు |
th | Thai | ไทย |
tr | Turkish | Türkçe |
uk | Ukrainian | Українська |
ur | Urdu | اردو |
vi | Vietnamese | Tiếng Việt |
cy | Welsh | Cymraeg |
yo | Yoruba | Yorùbá |
Refer to the ISO 639-1 codes for more options.
Step-by-step Tutorial
In this guide:
Section 1
: Prerequisites.Section 2
: Setup Cosmos Python Client.Section 3
: Translate text.Section 4
: Handle Errors.
1. Prerequisites
Before you begin, ensure you have:
- An active CosmosPlatform account
- API key from the API keys dashboard
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:
Get the Cosmos Python client through PIP:
pip install delos-cosmos
2.2. Authenticate Requests:
Initialize the client with your API key:
from cosmos import CosmosClient cosmos_client = CosmosClient(api_key=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() print(response)
3. Translate text
Initialize the client and perform your request (/translate_text
endpoint):
from cosmos import CosmosClient cosmos_client = CosmosClient(api_key="your-cosmos-api-key") response = cosmos_client.translate_text( text="Hello, how are you?", output_language="es" ) print(response)
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?" }
3.1. Parameters:
The parameters for the text translation request are:
Parameter | Description | Example |
---|---|---|
text | The text to translate | "Where is the nearest pharmacy?" |
output_language | The ISO 639-1 code of the target language | es-ES for Spanish |
input_language (optional) | The ISO 639-1 code of the original language | fr-FR for French |
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." }