Step By Step
1.创建阿里云账号并开通机器翻译服务,具体可参考https://help.aliyun.com/document_detail/310982.html;
2.获取阿里云账号的AccessKey ID 和AccessKey Secret(可通过菜单栏右上角控制台进入,再点击个人头像,进入Accesskey管理即可查看)如下图:
3.添加java依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alimt20181012</artifactId>
<version>1.0.3</version>
</dependency>
4.Java code
// This file is auto-generated, don't edit it. Thanks.
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.teautil.*;
import com.aliyun.teautil.models.*;
import com.google.gson.Gson;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.alimt20181012.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "mt.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("", "");
GetDetectLanguageRequest getDetectLanguageRequest = new GetDetectLanguageRequest()
.setSourceText("你好");
RuntimeOptions runtime = new RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
GetDetectLanguageResponse response = client.getDetectLanguageWithOptions(getDetectLanguageRequest, runtime);
System.out.println(new Gson().toJson(response));
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}