java的api文档列表翻译,快速入门:获取受支持语言的列表,Java - 文本翻译 API - Azure Cognitive Services | Azure Docs...

快速入门:使用 Java 通过文本翻译 API 获取受支持语言的列表Quickstart: Use the Translator Text API to get a list of supported languages using Java

07/23/2019

本文内容

在本快速入门中,请使用文本翻译 API 获取翻译、音译和字典查找支持的语言列表。In this quickstart, you get a list of languages supported for translation, transliteration, and dictionary lookup using the Translator Text API.

提示

如果你想一次看到所有代码,这个示例的源代码可以在 GitHub 上找到。If you'd like to see all the code at once, the source code for this sample is available on GitHub.

先决条件Prerequisites

使用 Gradle 初始化项目Initialize a project with Gradle

首先,创建此项目的工作目录。Let's start by creating a working directory for this project. 从命令行(或终端)中,运行以下命令:From the command line (or terminal), run this command:

mkdir get-languages-sample

cd get-languages-sample

接下来,初始化一个 Gradle 项目。Next, you're going to initialize a Gradle project. 此命令将创建 Gradle 的基本生成文件,最重要的是 build.gradle.kts,它在运行时用来创建并配置应用程序。This command will create essential build files for Gradle, most importantly, the build.gradle.kts, which is used at runtime to create and configure your application. 从工作目录运行以下命令:Run this command from your working directory:

gradle init --type basic

当提示你选择一个 DSL 时,选择 Kotlin。When prompted to choose a DSL, select Kotlin.

配置生成文件Configure the build file

找到 build.gradle.kts 并使用你喜欢使用的 IDE 或文本编辑器将其打开。Locate build.gradle.kts and open it with your favorite IDE or text editor. 然后将以下生成配置复制到其中:Then copy in this build configuration:

plugins {

java

application

}

application {

mainClassName = "GetLanguages"

}

repositories {

mavenCentral()

}

dependencies {

compile("com.squareup.okhttp:okhttp:2.5.0")

compile("com.google.code.gson:gson:2.8.5")

}

请注意,此示例依赖于 HTTP 请求的 OkHttp 以及 Gson 来处理和分析 JSON。Take note that this sample has dependencies on OkHttp for HTTP requests, and Gson to handle and parse JSON. 如果要详细了解生成配置,请参阅创建新的 Gradle 生成。If you'd like to learn more about build configurations, see Creating New Gradle Builds.

创建 Java 文件Create a Java file

为示例应用创建一个文件夹。Let's create a folder for your sample app. 从工作目录中,运行:From your working directory, run:

mkdir -p src/main/java

接下来,在此文件夹中,创建一个名为 GetLanguages.java 的文件。Next, in this folder, create a file named GetLanguages.java.

导入所需的库Import required libraries

打开 GetLanguages.java 并添加以下 import 语句:Open GetLanguages.java and add these import statements:

import java.io.*;

import java.net.*;

import java.util.*;

import com.google.gson.*;

import com.squareup.okhttp.*;

定义变量Define variables

首先,为你的项目创建一个公共类:First, you'll need to create a public class for your project:

public class GetLanguages {

// All project code goes here...

}

将以下行添加到 GetLanguages 类:Add these lines to the GetLanguages class:

String url = "https://api.translator.azure.cn/languages?api-version=3.0";

如果使用的是认知服务多服务订阅,则还必须在请求参数中包括 Ocp-Apim-Subscription-Region。If you are using a Cognitive Services multi-service subscription, you must also include the Ocp-Apim-Subscription-Region in your request parameters.

创建客户端并生成请求Create a client and build a request

将以下行添加到 GetLanguages 类来实例化 OkHttpClient:Add this line to the GetLanguages class to instantiate the OkHttpClient:

// Instantiates the OkHttpClient.

OkHttpClient client = new OkHttpClient();

接下来,我们将生成 GET 请求。Next, let's build the GET request.

// This function performs a GET request.

public String Get() throws IOException {

Request request = new Request.Builder()

.url(url).get()

.addHeader("Content-type", "application/json").build();

Response response = client.newCall(request).execute();

return response.body().string();

}

创建一个函数来分析响应Create a function to parse the response

这个简单的函数分析来自文本翻译服务的 JSON 响应并对其进行美化。This simple function parses and prettifies the JSON response from the Translator Text service.

// This function prettifies the json response.

public static String prettify(String json_text) {

JsonParser parser = new JsonParser();

JsonElement json = parser.parse(json_text);

Gson gson = new GsonBuilder().setPrettyPrinting().create();

return gson.toJson(json);

}

将其放在一起Put it all together

最后一步是发出请求并获得响应。The last step is to make a request and get a response. 将以下行添加你的项目:Add these lines to your project:

public static void main(String[] args) {

try {

GetLanguages getLanguagesRequest = new GetLanguages();

String response = getLanguagesRequest.Get();

System.out.println(prettify(response));

} catch (Exception e) {

System.out.println(e);

}

}

运行示例应用Run the sample app

上述操作完成后,就可以运行示例应用了。That's it, you're ready to run your sample app. 从命令行(或终端会话)导航到工作目录的根,然后运行以下命令:From the command line (or terminal session), navigate to the root of your working directory and run:

gradle build

当生成完成后,请运行:When the build completes, run:

gradle run

示例响应Sample response

请在此语言列表中查找国家/地区缩写。Find the country/region abbreviation in this list of languages.

成功的响应以 JSON 格式返回,如以下示例所示:A successful response is returned in JSON as shown in the following example:

{

"translation": {

"af": {

"name": "Afrikaans",

"nativeName": "Afrikaans",

"dir": "ltr"

},

"ar": {

"name": "Arabic",

"nativeName": "العربية",

"dir": "rtl"

},

...

},

"transliteration": {

"ar": {

"name": "Arabic",

"nativeName": "العربية",

"scripts": [

{

"code": "Arab",

"name": "Arabic",

"nativeName": "العربية",

"dir": "rtl",

"toScripts": [

{

"code": "Latn",

"name": "Latin",

"nativeName": "اللاتينية",

"dir": "ltr"

}

]

},

{

"code": "Latn",

"name": "Latin",

"nativeName": "اللاتينية",

"dir": "ltr",

"toScripts": [

{

"code": "Arab",

"name": "Arabic",

"nativeName": "العربية",

"dir": "rtl"

}

]

}

]

},

...

},

"dictionary": {

"af": {

"name": "Afrikaans",

"nativeName": "Afrikaans",

"dir": "ltr",

"translations": [

{

"name": "English",

"nativeName": "English",

"dir": "ltr",

"code": "en"

}

]

},

"ar": {

"name": "Arabic",

"nativeName": "العربية",

"dir": "rtl",

"translations": [

{

"name": "English",

"nativeName": "English",

"dir": "ltr",

"code": "en"

}

]

},

...

}

}

后续步骤Next steps

查看 API 参考,了解使用文本翻译 API 可以执行的所有操作。Take a look at the API reference to understand everything you can do with the Translator Text API.

另请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值