Java SDK

Updated at:

This document explains how to call the Machine Translation Java SDK. You will learn how to:

  • Set up your account

  • Configure the Java environment

  • Call the Java SDK

Procedure

Step 1: Activate service and obtain key

  1. Register an account: If you do not have an Alibaba Cloud account, register one. For more information, see Register an account.

  2. Activate the Machine Translation service: Go to the Machine Translation service page, select the service you want to activate, and click Activate Now. For more information about billing, see the Billing documentation.

    In the Text Translation section, you can select General-purpose Translation (supports 214 languages) or Domain-specific Translation (for e-commerce, social, and medical domains). Click Activate Now on the corresponding card.

  3. Obtain an AccessKey: Log on to the Alibaba Cloud AccessKey page to get your AccessKey ID and AccessKey Secret. For more information, see Prepare your account.

    On the AccessKey page, you can view your existing AccessKey IDs and their enabled/disabled status, or click Create AccessKey to generate a new key.

Step 2: Configure Java environment

  1. Check your Java version

    To use the Machine Translation SDK, you need Java 8 or later. You can run the following command in your terminal to check your Java version:

    java -version

    Check the first line of the output to confirm your Java version. For example, the output openjdk version "16.0.1" 2021-04-20 indicates that the Java version is 16. If Java is not installed, or if your version is earlier than Java 8, download and install it from Java Download.

  2. Add a dependency

    Add the following dependency to your pom.xml file. Replace the-latest-version with the latest version number. You can find the latest version by following the link in the code comments.

    <!-- https://mvnrepository.com/artifact/com.aliyun/alimt20181012 -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>alimt20181012</artifactId>
      <version>the-latest-version</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.aliyun/tea-openapi -->
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-openapi</artifactId>
      <version>the-latest-version</version>
    </dependency>

Step 3: Call Machine Translation SDK

The following code shows how to call the Machine Translation SDK by using the TranslateGeneral API.

Important

Replace ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET in the code with your AccessKey ID and AccessKey Secret from Step 1: Activate service and obtain key.

package com.aliyun.sample;
import com.aliyun.tea.*;
import com.aliyun.alimt20181012.*;
import com.aliyun.alimt20181012.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.darabonba.env.*;
import com.aliyun.teaconsole.*;
public class Sample {
    public static com.aliyun.alimt20181012.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // Replace ALIBABA_CLOUD_ACCESS_KEY_ID with your AccessKey ID.
                .setAccessKeyId ("ALIBABA_CLOUD_ACCESS_KEY_ID")
                // Replace ALIBABA_CLOUD_ACCESS_KEY_SECRET with your AccessKey Secret.
                .setAccessKeySecret ("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        // For endpoints, see https://api.aliyun.com/product/alimt
        config.endpoint = "mt.cn-hangzhou.aliyuncs.com";
        return new com.aliyun.alimt20181012.Client(config);
    }
    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.alimt20181012.Client client = Sample.createClient();
        TranslateGeneralRequest request = new TranslateGeneralRequest()
                .setFormatType("text")
                .setSourceLanguage("zh")
                .setTargetLanguage("en")
                .setSourceText("你好")
                .setScene("general");
        TranslateGeneralResponse response = client.translateGeneral(request);
        com.aliyun.teaconsole.Client.log(response.body.data.translated);
    }
 }

Output:

Hello
Process finished with exit code 0

For more API examples, visit the Machine Translation API documentation.