# @azure-rest/ai-translation-text 中文文档

规范地址：https://npmcn.com/package/%40azure-rest/ai-translation-text/

版本：2\.0\.0
npm 元数据更新时间：2026-05-28T20:17:14.359Z
中文译文更新时间：2026-11-07T12:35:21.296Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40azure-rest/ai-translation-text
原文对照：https://npmcn.com/package/%40azure-rest/ai-translation-text/?lang=original
项目主页：https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/translation/ai-translation-text-rest/README.md
源码仓库：https://github.com/Azure/azure-sdk-for-js

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：416 kB，186 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=20\.0\.0
- 运行依赖：5 个
- 首次发布：2023-04-14T11:46:20.627Z
- 最新版本发布：2026-05-28T20:17:14.153Z
- 近 30 天下载量：389040（2026-10-08 至 2026-11-06，npm 官方下载统计）
- 已发布版本数：371（近 12 个月 59 个）

## 中文简介

Azure 翻译处的同位素客户端库

## 安装

```sh
npm install @azure-rest/ai-translation-text
```

也可以使用：`pnpm add @azure-rest/ai-translation-text`、`yarn add @azure-rest/ai-translation-text`、`bun add @azure-rest/ai-translation-text`

## README

# JavaScript 的 Azure 文本翻译 REST 客户端库

Azure文本翻译是由Azure翻译服务提供的一种以云为基础的REST API. 它利用神经机翻译技术,在所有支持的语文中提供准确、贴切和准确的实时文本翻译.

客户端库提供几个关键功能:

-   获取用于翻译和转写操作的支持语言列表,以及可用于翻译的LLM模型.
    
-   执行从指定源语言到目标语言的决定性文本翻译,带有可配置参数,以确保精确性并保持上下文完整性.
    
-   通过将文字从原始脚本转换为可替代的脚本表示来进行转写.
    
-   使用LLM模型来制作有调子和性别意识的翻译输出变体.
    

**请多多依靠我们 [REST 客户端文件](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) 使用此库**

密钥链接 :

-   [源代码](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/translation/ai-translation-text-rest)
-   [软件包( NPM)](https://www.npmjs.com/package/@azure-rest/ai-translation-text)
-   [API 参考文件](https://learn.microsoft.com/azure/ai-services/translator/text-translation/preview/rest-api-guide)
-   [样本](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/translation/ai-translation-text-rest/samples)

## 开始

### 目前得到支持的环境

-   节点.js的LTS版本
-   Edge、Chrome、Safari和Firefox的最新版本

### 先决条件

-   你一定有一个 [Azure 订阅](https://azure.microsoft.com/free/) 用于使用此软件包.
-   现有翻译服务或认知服务资源.

### 安装 `@azure-rest/ai-translation-text` 软件包

为 JavaScript 安装 Azure 文本翻译 REST 客户端 REST 客户端库 `npm`数字 :

```bash
npm install @azure-rest/ai-translation-text
```

#### 创建翻译服务资源

您可以在下面创建翻译资源 [创建翻译资源](https://learn.microsoft.com/azure/ai-services/translator/how-to/create-translator-resource?tabs=foundry)。 。 。 .

### 浏览器支持

#### Java 脚本套装

要在浏览器中使用此客户端库,首先需要使用捆绑器. 关于如何做到这一点的详情,请参见我们 [捆绑文件](https://aka.ms/AzureSDKBundling)。 。 。 .

### 认证客户端

使用客户端库与服务的互动始于创建实例 [文本翻译](https://learn.microsoft.com/javascript/api/@azure-rest/ai-translation-text/texttranslationclient) 课。 你需要一个 **API 密钥** 或者说 `TokenCredential` 以即时化客户端对象。 关于使用认知服务认证的更多信息,请参见: [向翻译处认证请求](https://learn.microsoft.com/azure/ai-services/translator/text-translation/reference/authentication)。 。 。 .

#### 获取 API 密钥

你可以得到这个 `endpoint`, (中文(简体) ), `API key` 财务报告和审定财务报表 `Region` 翻译服务资源信息 [Azure 门户](https://portal.azure.com/)。 。 。 .

或者,使用 [阿苏尔CLI](https://learn.microsoft.com/cli/azure) 下面的片段可以从翻译服务资源获取 API 密钥.

```PowerShell
az cognitiveservices account keys list --resource-group <your-resource-group-name> --name <your-resource-name>
```

#### 创建一个 `TextTranslationClient` 使用 Azure 活动目录证书

用一个 [Azure 活动目录( AAD) 证书](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token), (中文(简体) ), 提供从下列来源获得的所需证书类型的实例: [@azure/身份](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) 图书馆.

要认证 AAD , 您必须首先 `npm` 安装 [`@azure/identity`](https://www.npmjs.com/package/@azure/identity)

设置后,您可以选择哪种类型 [证书](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) 从 `@azure/identity` 用来使用. 举例来说, [默认标记](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential) 可用于认证客户端.

### 创建一个 `TextTranslationClient` 使用 API 密钥和区域证书

一旦您对 API 密钥和区域有值, 请创建一个 `TranslatorCredential`。 。 。 .

与价值 `TranslatorCredential` 您可以创建 [文本翻译](https://learn.microsoft.com/javascript/api/@azure-rest/ai-translation-text/texttranslationclient)数字 :

```ts
import TextTranslationClient, { TranslatorCredential } from "@azure-rest/ai-translation-text";

const endpoint = "https://api.cognitive.microsofttranslator.com";
const key = "YOUR_SUBSCRIPTION_KEY";
const region = "westus";
const credential: TranslatorCredential = {
  key,
  region,
};
const translationClient = TextTranslationClient(endpoint, credential);
```

## 实例

下一节提供了若干使用 `client` [上方创建](https://npmcn.com/package/%40azure-rest/ai-translation-text/#readme-create-a-texttranslationclient-using-an-api-key-and-region-credential),并覆盖此客户端库中的主要功能.

### 获得支持的语言

获得翻译器其他操作目前支持的一组语言.

```ts
import TextTranslationClient, {
  TranslatorCredential,
  isUnexpected,
} from "@azure-rest/ai-translation-text";

const endpoint = "https://api.cognitive.microsofttranslator.com";
const key = "YOUR_SUBSCRIPTION_KEY";
const region = "westus";
const credential: TranslatorCredential = {
  key,
  region,
};
const translationClient = TextTranslationClient(endpoint, credential);

const langResponse = await translationClient.path("/languages").get();

if (isUnexpected(langResponse)) {
  throw langResponse.body.error;
}

const languages = langResponse.body;

if (languages.translation) {
  console.log("Translated languages:");
  for (const [key, translationLanguage] of Object.entries(languages.translation)) {
    console.log(`${key} -- name: ${translationLanguage.name} (${translationLanguage.nativeName})`);
  }
}

if (languages.transliteration) {
  console.log("Transliteration languages:");
  for (const [key, transliterationLanguage] of Object.entries(languages.transliteration)) {
    console.log(
      `${key} -- name: ${transliterationLanguage.name} (${transliterationLanguage.nativeName})`,
    );
  }
}

if (languages.models) {
  console.log("Available LLM Models:");
  for (const model in languages.models) {
    console.log(model);
  }
}
```

请参考服务文件,以进行概念性讨论 [语言](https://learn.microsoft.com/azure/ai-services/translator/text-translation/preview/get-languages)。 。 。 .

### 翻译

将单一源语言文本与多个目标语言文本合并,并同时提出单一请求.

```ts
import TextTranslationClient, {
  TranslatorCredential,
  isUnexpected,
} from "@azure-rest/ai-translation-text";

const endpoint = "https://api.cognitive.microsofttranslator.com";
const key = "YOUR_SUBSCRIPTION_KEY";
const region = "westus";
const credential: TranslatorCredential = {
  key,
  region,
};
const translationClient = TextTranslationClient(endpoint, credential);

const input = {
  text: "This is a test.",
  targets: [{ language: "cs" }],
  language: "en",
};
const translateResponse = await translationClient.path("/translate").post({
  body: { inputs: [input] },
});

if (isUnexpected(translateResponse)) {
  throw translateResponse.body.error;
}

const translations = translateResponse.body.value;
for (const translation of translations) {
  console.log(
    `Text was translated to: '${translation?.translations[0]?.language}' and the result is: '${translation?.translations[0]?.text}'.`,
  );
}
```

请参考服务文件,以进行概念性讨论 [翻译](https://learn.microsoft.com/azure/ai-services/translator/text-translation/preview/translate-api)。 。 。 .

### 翻译

将源语言的字符或字母转换为目标语言的相应字符或字母.

```ts
import TextTranslationClient, {
  TranslatorCredential,
  isUnexpected,
} from "@azure-rest/ai-translation-text";

const endpoint = "https://api.cognitive.microsofttranslator.com";
const key = "YOUR_SUBSCRIPTION_KEY";
const region = "westus";
const credential: TranslatorCredential = {
  key,
  region,
};
const translationClient = TextTranslationClient(endpoint, credential);

const inputText = [{ text: "这是个测试。" }];
const parameters = {
  language: "zh-Hans",
  fromScript: "Hans",
  toScript: "Latn",
};
const transliterateResponse = await translationClient.path("/transliterate").post({
  body: { inputs: inputText },
  queryParameters: parameters,
});

if (isUnexpected(transliterateResponse)) {
  throw transliterateResponse.body.error;
}

const transliterations = transliterateResponse.body.value;
for (const transliteration of transliterations) {
  console.log(
    `Input text was transliterated to '${transliteration?.script}' script. Transliterated text: '${transliteration?.text}'.`,
  );
}
```

请参考服务文件,以进行概念性讨论 [转写](https://learn.microsoft.com/azure/ai-services/translator/text-translation/preview/transliterate-api)。 。 。 .

## 解决问题

当您使用文本翻译客户端库与翻译处互动时,翻译处返回的错误对应为REST API请求返回的相同的HTTP状态代码.

例如,如果您提交翻译请求时没有目标翻译语言,a `400` 返回错误,表示“坏请求”.

### 日志

允许伐木可能有助于发现有关失败的有用信息。 为了查看 HTTP 请求和回复的日志,设置 `AZURE_LOG_LEVEL` 环境变量改为 `info`。或者,可以在运行时拨打 `setLogLevel` 输入 `@azure/logger`数字 :

```ts
import { setLogLevel } from "@azure/logger";

setLogLevel("info");
```

关于如何启用日志的更详细指示,您可以查看 [@ azure/ logger 包文件](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/core/logger)。 。 。 .

## 常见问题

### @azure-rest/ai-translation-text 是什么？

Azure 翻译处的同位素客户端库

### 如何安装 @azure-rest/ai-translation-text？

在项目目录运行 npm install @azure-rest/ai-translation-text，或使用 pnpm add / yarn add / bun add。

### @azure-rest/ai-translation-text 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @azure-rest/ai-translation-text 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @azure-rest/ai-translation-text 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=20\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @azure-rest/ai-translation-text 可以商用吗？

@azure-rest/ai-translation-text 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @azure-rest/ai-translation-text 的下载量有多少？

根据 npm 官方下载统计，@azure-rest/ai-translation-text 在 2026/10/08 – 2026/11/06 这 30 天内被下载 389,040 次，日均约 12,968 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @azure-rest/ai-translation-text 还在维护吗？

最新版本 2\.0\.0 发布于 2026/05/29（5 个月前）；至今共发布 371 个版本，其中过去 12 个月发布了 59 个；首次发布于 2023/04/14。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

