Python SDK

Updated at:

The Machine Translation Python SDK lets you translate text programmatically. Two API variants are available: the dedicated version (optimized for e-commerce content) and the general version (for all other content).

Prerequisites

Before you begin, make sure you have:

  • Python 2.6.x, 2.7.x, 3.x, and later

  • An Alibaba Cloud account with real-name verification completed

  • An AccessKey pair (AccessKey ID and AccessKey secret)

Install the SDK

Install the SDK using pip (recommended):

pip install alibabacloud_alimt20181012==1.1.0
Note: For Python 3.x, install aliyun-python-sdk-core-v3 instead of aliyun-python-sdk-core when installing the core library from source.

To install from source instead, clone the repository and run:

git clone https://github.com/aliyun/aliyun-openapi-python-sdk.git
cd aliyun-openapi-python-sdk/aliyun-python-sdk-core
python setup.py install
cd ../aliyun-python-sdk-alimt
python setup.py install

Get an AccessKey pair

An AccessKey pair identifies your Alibaba Cloud account when making API requests. It consists of an AccessKey ID and an AccessKey secret.

  1. Log on to the RAM console.

  2. Create an AccessKey pair or select an existing one.

  3. Make sure the AccessKey pair is in the Enabled state.

Store your credentials in environment variables rather than hard-coding them in source code:

export ALIBABA_CLOUD_ACCESS_KEY_ID=<your-access-key-id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<your-access-key-secret>

Make a translation call

All examples use AcsClient to initialize a client and do_action_with_exception to send requests. The response is a JSON-encoded byte string — parse it with json.loads() to access the result.

Dedicated version

Use the dedicated version (TranslateRequest) for e-commerce content. It accepts a scene parameter that optimizes translation for specific content types.

  1. Create a file named translate_dedicated.py with the following code:

    #coding: utf-8
    import json
    import os
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkalimt.request.v20181012 import TranslateRequest
    
    # Load credentials from environment variables
    client = AcsClient(
        os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
        os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
        "cn-hangzhou"
    )
    
    # Build the translation request
    request = TranslateRequest.TranslateRequest()
    request.set_SourceLanguage("en")
    request.set_TargetLanguage("zh")
    request.set_SourceText("hello")
    request.set_FormatType("text")
    request.set_Scene("title")  # Valid values: title, description, communication
    request.set_method("POST")
    
    # Send the request and print the response
    response = client.do_action_with_exception(request)
    print(json.loads(response))
  2. Run the file:

    python translate_dedicated.py

The scene parameter accepts the following values:

ValueContent type
titleProduct title
descriptionProduct description
communicationProduct communication

General version

Use the general version (TranslateGeneralRequest) for everyday translation without content-type optimization.

  1. Create a file named translate_general.py with the following code:

    #coding: utf-8
    import json
    import os
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkalimt.request.v20181012 import TranslateGeneralRequest
    
    # Load credentials from environment variables
    client = AcsClient(
        os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID"),
        os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
        "cn-hangzhou"
    )
    
    # Build the translation request
    request = TranslateGeneralRequest.TranslateGeneralRequest()
    request.set_SourceLanguage("en")
    request.set_TargetLanguage("zh")
    request.set_SourceText("hello")
    request.set_FormatType("text")
    request.set_method("POST")
    
    # Send the request and print the response
    response = client.do_action_with_exception(request)
    print(json.loads(response))
  2. Run the file:

    python translate_general.py

Parameters

ParameterMethodDescriptionExample
Source languageset_SourceLanguage()Language code of the input text"en"
Target languageset_TargetLanguage()Language code to translate into"zh"
Source textset_SourceText()Text to translate"hello"
Format typeset_FormatType()Input format"text"
Sceneset_Scene()Content type optimization (dedicated version only). Valid values: title, description, communication"title"