Step By Step
1、创建阿里云账号并开通机器翻译服务,具体可参考https://help.aliyun.com/document_detail/310982.html;
2、获取阿里云账号的AccessKey ID 和AccessKey Secret(可通过菜单栏右上角控制台进入,再点击个人头像,进入Accesskey管理即可查看)如下图;
3、创建Maven项目,添加Pom依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.13</version>
</dependency>
4、Java Code Sample
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
//机器翻译-机器翻译通用版调用指南
public class TranslateGeneralCommon {
public static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<AccessKey ID>", "<AccessKey Secret>");
public static DefaultAcsClient client = new DefaultAcsClient(profile);
public static void main(String[] args) {
CommonRequest request = new CommonRequest();
request.setSysDomain("mt.aliyuncs.com");
request.setSysAction("TranslateGeneral");
request.setSysVersion("2018-10-12");
request.setSysMethod(MethodType.POST);
request.putQueryParameter("FormatType","text");
request.putQueryParameter("Scene","general");
request.putQueryParameter("SourceLanguage","zh");
request.putQueryParameter("TargetLanguage","en");
request.putQueryParameter("SourceText","你好");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ClientException e) {
e.printStackTrace();
}
}
}
5、运行结果
注意事项
关于version版本参数,可通过阿里云机器翻译通用版原版SDK中的Node.js查看: