Translate
Let's dive into how to translate using our translation-API!
NOTE!
- Please note this API is due for a rework. If you are encountering any issues, please check this page again for any changes!
- API-keys are currently not being distributed. This means that this documentation version will not include API keys in the API calls.
Requirements
Calling this API endpoint requires a valid API key that you can generated on the Translation-API dashboard (SOON) if there are over 20,000 queries a month. The amount of queries a month are bound to the IPV4 of a user, so if there are less than 20,000 queries a month you should not receive any refused responses.
Parameters
Calling the http://translationapi.assistantslab.com/translate endpoint requires a total of 3 parameters:
text→ This is your text you would like to translatesource_lang→ This is your source language ID (example: 'EN')target_lang→ This is your target language ID (example: 'EN')
Please ensure you are using the language ID's mentioned in the intro. This follows the languages codes standardized in Set 1 of ISO 639.
Example API call
Python
example.py
import requests
import json
def translate(text, source_lang, target_lang):
api_url = "http://translationapi.assistantslab.com/translate"
# Create the JSON payload
json_payload = {"text": text, "source_lang": source_lang, "target_lang": target_lang}
# Send a POST request to the API
response = requests.post(api_url, json=json_payload)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
response_json = response.json()
# Extract the translation
translation = response_json.get("translation")
return translation
else:
print("Error:", response.text)
return None
translator.java
public void translate(String text, String sourceLang, String targetLang) {
try {
String apiUrl = "http://translationapi.assistantslab.com/translate";
// Create a URL object with the API endpoint
URL url = new URL(apiUrl);
// Open a connection to the URL
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set the request method to POST
connection.setRequestMethod("POST");
// Enable input/output streams
connection.setDoOutput(true);
connection.setDoInput(true);
// Set the content type to JSON
connection.setRequestProperty("Content-Type", "application/json");
// Create JSON payload
String jsonInputString = "{\"text\":\"" + text + "\",\"source_lang\":\"" + sourceLang
+ "\",\"target_lang\":\"" + targetLang + "\"}";
// Write the JSON payload to the connection's output stream
try (OutputStream os = connection.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
// Get the response from the server
StringBuilder response = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
}
// Parse JSON
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(response.toString(), JsonObject.class);
// Extract translation value
JsonElement translationElement = jsonObject.get("translation");
String translation = "";
if (translationElement != null) {
if (translationElement.isJsonArray()) {
JsonArray translationArray = translationElement.getAsJsonArray();
if (!translationArray.isEmpty()) {
translation = translationArray.get(0).getAsString();
}
} else if (translationElement.isJsonPrimitive()) {
translation = translationElement.getAsString();
}
}
return translation;
} catch (Exception e) {
e.printStackTrace();
}
}
Issues
Are you experiencing any issues calling our API? Feel free to contact our support team.