Doctranslate.io Translation API
  • Introduction
    • Welcome to Doctranslate.io
    • What is DocTranslate.io
    • User Manual
  • Document Translation API
    • System requirement
    • Upload API
      • Upload multiple files and calculate usage cost
    • User API
      • User History
    • Translation API
      • API V1
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Image Translation
        • Audio Translation
        • Text Translation
        • Get result
      • API V2
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Image Translation
        • Audio Translation
        • Text Translation
        • Get result
      • API V3
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Audio Translation
        • Text Translation
        • Get result
    • Presentation Maker API
      • API V1
        • Presentation Maker
        • Presentation Maker For Multiple Files
      • API V2
        • Presentation Maker
        • Presentation Maker For Multiple Files
  • Presentation Video API
    • API V1
      • Create transcripts presentation
      • Update transcripts presentation
      • Create video presentation
      • Get result
    • API V2
      • Create transcripts presentation
      • Update transcripts presentation
      • Create video presentation
      • Get result
  • Others
    • Code examples
      • API V1
        • Upload Multiple Files
        • Translate Multiple Document Files
        • Text Translation
        • Document Translation
        • Image Translation
        • Audio Translation
        • Video Translation
        • Presentation Maker
        • Presentation Video
      • API V2
        • Upload Multiple Files
        • Translate Multiple Document Files
        • Text Translation
        • Document Translation
        • Image Translation
        • Audio Translation
        • Video Translation
        • Presentation Maker
        • Presentation Video
    • Support
Powered by GitBook
On this page
  1. Others
  2. Code examples
  3. API V1

Text Translation

import requests
import datetime
from time import sleep
import json

# Endpoints
BASE_URL = "https://doctranslate-api.doctranslate.io"
BE_TRANSLATE_URL = f"{BASE_URL}/v1/translate/text"
BE_GET_RESULT_URL = f"{BASE_URL}/v1/result/"
DEST_LANG = 'vi'

# Authentication token
auth_token = "<YOUR_API_TOKEN>"

TEXT = "Hello, my name is Thanh. I am a software engineer."

DICTIONARY = json.dumps(
[
    {
        "ori_lang": "en",
        "des_lang": "vi",
        "ori_word": "hello",
        "des_word": "Tạm biệt",
    },
    {
        "ori_lang": "en",
        "des_lang": "vi",
        "ori_word": "I",
        "des_word": "Tớ",
    },
])

# Function to post translation request
def post_translation(text, dest_lang, token):
    form_data = {
        'original_lang': None,  # Assuming server handles 'None' as auto-detect
        'dest_lang': dest_lang,
        'process_mode': 'replace',
        'translate_type': 'Professional',
        'text': text,
        'dictionary': DICTIONARY
    }
    headers = {'Authorization': f'Bearer {token}'}
    response = requests.post(BE_TRANSLATE_URL, data=form_data, headers=headers)
    return response.json()

# Function to check translation result
def check_translation(task_id, token, timeout=600):
    start_time = datetime.datetime.now()
    while (datetime.datetime.now() - start_time).seconds < timeout:
        response = requests.get(f"{BE_GET_RESULT_URL}{task_id}", headers={'Authorization': f'Bearer {token}'})
        response_data = response.json()
        if response_data['status'] == 'success' and 'data' in response_data and 'url_download' in response_data['data'] and response_data['data']['url_download'] != '':
            return response_data['data']
        sleep(5)
    return None

# Main execution
response_data = post_translation(TEXT, DEST_LANG, auth_token )
if response_data['status'] == 'success':
    task_id = response_data['data']['task_id']
    print(f'Task ID: {task_id}')
    result_data = check_translation(task_id, auth_token)
    if result_data and 'url_download' in result_data:
        print(f"URL Download: {result_data['url_download']}")
        print(f"\nText: {result_data['text']}")
    else:
        print('Error: No download URL found within the given timeout.')
else:
    print('Error: Failed to initiate translation task. Please check your request and try again.')

PreviousTranslate Multiple Document FilesNextDocument Translation

Last updated 1 year ago