@azure-rest/ai-translation-text

中文文档
2.0.0发布于 2026/05/29MITTypeScriptESM + CJS近 30 天下载 38.9万

An isomorphic client library for the Azure Translator Service

原始使用文档
文档目录13 节
  1. Getting started
  2. Currently supported environments
  3. Prerequisites
  4. Install the @azure-rest/ai-translation-text package
  5. Browser support
  6. Authenticate the client
  7. Create a TextTranslationClient using an API key and Region …
  8. Examples
  9. Get Supported Languages
  10. Translate
  11. Transliterate
  12. Troubleshooting
  13. Logging

Azure TextTranslation REST client library for JavaScript

Azure text translation is a cloud-based REST API provided by the Azure Translator service. It utilizes neural machine translation technology to deliver precise, contextually relevant, and semantically accurate real-time text translations across all supported languages.

The client library offers several key functionalities:

  • Retrieve the list of languages supported for translation and transliteration operations, as well as LLM models available for translations.

  • Perform deterministic text translation from a specified source language to a target language, with configurable parameters to ensure precision and maintain contextual integrity.

  • Execute transliteration by converting text from the original script to an alternative script representation.

  • Use LLM models to produce translation output variants that are tone-specific and gender-aware.

Please rely heavily on our REST client docs to use this library

Key links:

Getting started

Currently supported environments

  • LTS versions of Node.js
  • Latest versions of Edge, Chrome, Safari and Firefox

Prerequisites

  • You must have an Azure subscription to use this package.
  • An existing Translator service or Cognitive Services resource.

Install the @azure-rest/ai-translation-text package

Install the Azure TextTranslation REST client REST client library for JavaScript with npm:

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

Create a Translator service resource

You can create a Translator resource following Create a Translator resource.

Browser support

JavaScript Bundle

To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our bundling documentation.

Authenticate the client

Interaction with the service using the client library begins with creating an instance of the TextTranslationClient class. You will need an API key or TokenCredential to instantiate a client object. For more information regarding authenticating with cognitive services, see Authenticate requests to Translator Service.

Get an API key

You can get the endpoint, API key and Region from the Cognitive Services resource or Translator service resource information in the Azure Portal.

Alternatively, use the Azure CLI snippet below to get the API key from the Translator service resource.

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

Create a TextTranslationClient using an Azure Active Directory credential

To use an Azure Active Directory (AAD) token credential, provide an instance of the desired credential type obtained from the @azure/identity library.

To authenticate with AAD, you must first npm install @azure/identity

After setup, you can choose which type of credential from @azure/identity to use. As an example, DefaultAzureCredential can be used to authenticate the client.

Create a TextTranslationClient using an API key and Region credential

Once you have the value for the API key and Region, create an TranslatorCredential.

With the value of the TranslatorCredential you can create the TextTranslationClient:

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);

Examples

The following section provides several code snippets using the client created above, and covers the main features present in this client library.

Get Supported Languages

Gets the set of languages currently supported by other operations of the Translator.

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);
  }
}

Please refer to the service documentation for a conceptual discussion of languages.

Translate

Renders single source-language text to multiple target-language texts with a single request.

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}'.`,
  );
}

Please refer to the service documentation for a conceptual discussion of translate.

Transliterate

Converts characters or letters of a source language to the corresponding characters or letters of a target language.

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}'.`,
  );
}

Please refer to the service documentation for a conceptual discussion of transliterate.

Troubleshooting

When you interact with the Translator Service using the TextTranslator client library, errors returned by the Translator service correspond to the same HTTP status codes returned for REST API requests.

For example, if you submit a translation request without a target translate language, a 400 error is returned, indicating "Bad Request".

Logging

Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the AZURE_LOG_LEVEL environment variable to info. Alternatively, logging can be enabled at runtime by calling setLogLevel in the @azure/logger:

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

setLogLevel("info");

For more detailed instructions on how to enable logs, you can look at the @azure/logger package docs.

版本与发布记录

@azure-rest/ai-translation-text 共发布 371 个版本,首次发布于 2023/04/14;过去 12 个月发布了 59 个版本。

版本发布日期距今
2.0.0latest2026/05/295 个月前
2.0.0-alpha.20260211.1dev2026/02/118 个月前
2.0.0-alpha.20260210.12026/02/108 个月前
2.0.0-alpha.20260209.12026/02/098 个月前
2.0.0-alpha.20260206.12026/02/069 个月前
2.0.0-alpha.20260205.12026/02/059 个月前

其他发布通道:beta → 2.0.0-beta.1,dev → 2.0.0-alpha.20260211.1。

关于 @azure-rest/ai-translation-text 的常见问题

@azure-rest/ai-translation-text 是什么?

An isomorphic client library for the Azure Translator Service

如何安装 @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 和提交记录判断。