All Products
Search
Document Center

Machine Translation:Java SDK

Last Updated:Mar 14, 2023

Download links

The Machine Translation Java SDK allows Java developers to easily use Java programs to operate Alibaba Cloud Machine Translation services. You can use Maven dependencies to add the SDK. You can also download the SDK package to an on-premises server. Currently, the SDK supports J2SE 6.0 and above. Click here to download the latest version of the Machine Translation SDK. You need to download the latest version of the Alibaba Cloud SDK.

Procedure

To quickly start using the Machine Translation Java SDK, perform the following steps:

Step 1 Create an Alibaba Cloud account

For more information, see Alibaba Cloud account registration process.

We recommend that you complete real-name verification at your earliest opportunity. Otherwise, you cannot use some Alibaba Cloud services.

Step 2 Obtain an Alibaba Cloud access key

To use the Machine Translation Java SDK, you must apply for an AccessKey of Alibaba Cloud.

Log on to the Key Management page of Alibaba Cloud. Select an AccessKey pair for the SDK. If no AccessKey pair is available, create one and make sure that it is in the Enable state. Keys refer to Access Key ID and Access Key Secret

The AccessKey pair will be used in the following steps and must be kept confidential.

Step 3 Install the Java development environment

Currently, the Machine Translation SDK supports the Java runtime environment of J2SE 6.0 or later. You can download the Java development environment from the Java official website and follow the instructions to install the Java development environment.

Step 4 Install the Machine Translation Java SDK

After you install the Java development environment, you must install Log Service SDK for Java. Currently, we provide two ways to install the Java SDK for Machine Translation services:

  1. Download the latest SDK for Java package from Apache Maven. Add the following configurations to your Maven project:

     
    <dependency> 
        <groupId>com.aliyun</groupId> 
        <artifactId>aliyun-java-sdk-core</artifactId> 
        <version>4.3.2</version> 
    </dependency> 
    <dependency> 
       <groupId>com.aliyun</groupId> 
       <artifactId>aliyun-java-sdk-alimt</artifactId> 
       <version>1.0.3</version> 
    </dependency>
  2. Download Log Service SDK for Java package, and then reference the local package in your Java project.

    1. To clone the Java SDK package from here, you need to download the latest version of Alibaba Cloud SDK (the version is updated regularly. If you want to use the latest version, use Maven).

    2. Decompress the downloaded package to a specified directory. Log Service SDK for Java does not require installation.

    3. Add all JAR packages (including third-party dependent packages) in the SDK package to your Java project. For more information, see the relevant documents about integrated development environment (IDE).

Step 5 Start a new Java project

Now, you can start using the SDK. Use any text editor or Java IDE and run the following sample code to interact with the Machine Translation server and obtain the corresponding output. For more information about how to use the Java SDK, see the section "Additional considerations" in this topic.

JAVA code example

 
package com.cloud; 
 
import com.alibaba.fastjson.JSONObject; 
import com.aliyuncs.DefaultAcsClient; 
import com.aliyuncs.IAcsClient; 
import com.aliyuncs.alimt.model.v20181012.TranslateECommerceRequest; 
import com.aliyuncs.alimt.model.v20181012.TranslateECommerceResponse; 
import com.aliyuncs.http.MethodType; 
import com.aliyuncs.profile.DefaultProfile; 
 
import java.net.URLEncoder; 
 
public class TranslateTest{ 
    public static void main(String[ ]args){ 
        String accessKeyId="<your_access_key_id>";// Use your Alibaba Cloud AccessKeyId. 
    String accessKeySecret="<your_access_key_secret>";// Use your Alibaba Cloud AccessKey 
    // Create a DefaultAcsClient instance and initialize it. 
    try{ 
        DefaultProfile profile=DefaultProfile.getProfile( "cn-hangzhou",// The region ID. 
        accessKeyId,// The AccessKey ID of the Alibaba Cloud account. 
        accessKeySecret);// Alibaba Cloud account Access Key Secret 
        IAcsClient client=new DefaultAcsClient(profile); 
        // Create an API request and set the request parameters. 
        TranslateECommerceRequest eCommerceRequest=new TranslateECommerceRequest();                   
        eCommerceRequest.setScene("title"); 
        eCommerceRequest.setMethod(MethodType.POST);// Set the request method, POST. 
        eCommerceRequest.setFormatType("text");// The format of the translated text. 
        eCommerceRequest.setSourceLanguage("zh");// Source language 
        eCommerceRequest.setSourceText(URLEncoder.encode("Hello","UTF-8"));// Original text 
        eCommerceRequest.setTargetLanguage("en");// Target language 
        TranslateECommerceResponse eCommerceResponse=client.getAcsResponse(eCommerceRequest); 
         System.out.println(JSONObject.toJSON(eCommerceResponse)); 
      }catch(Exception e){ 
         e.printStackTrace(); 
      } 
   } 
}