npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@kabeep/node-translate

v1.2.0

Published

A powerful, secure and feature-rich api via Google Translation.

Readme

一个强大、安全且功能丰富的 API,通过 Google 翻译。


NodeJS License NPM Codecov Codacy CircleCI

English | 简体中文

Alt

📖 简介

感谢 matheussiamtraction 编写了该库的原始版本。由于原始版本作者不再积极维护,我使用 Typescript 和 ISO-639-1 重写了该库,使程序变得安全提供更丰富地翻译结果,并解决了程序异常。

什么是新的?

  • 自适应本机语言翻译
  • 近义词、多义解释和例句
  • 复杂网络中的超时与重试参数
  • 支持语言、语言代码、母语和自适应语言列表查询
  • 支持 iso-639-1、小语种、特殊语种和 Wikipedia 上最新的变更
  • 安全的 API 类型和完整覆盖率
  • 可控的错误状态

⚙️ 安装

npm install @kabeep/node-translate --save
yarn add @kabeep/node-translate
pnpm add @kabeep/node-translate

🚀 使用

import translate from '@kabeep/node-translate';

函数: translate(text, options)

translate(text, options)
    .then(console.log)
    .catch(console.error);

| 参数 | 类型 | 可选 | 默认值 | 描述 | |-------------------|--------------------|-----|----------|------------------------------------------| | text | string | 否 | - | 源文本、短语或单词。 | | options | TranslateOptions | - | - | 翻译选项。 | | options.from | LanguageCode | 是 | 'auto' | 要从中翻译的语言名称/ISO 639-1代码。如果未指定,则会自动检测源语言。 | | options.to | LanguageCode | 是 | 'auto' | 要翻译到的语言名称/ISO 639-1代码。如果未指定,则会翻译为本机环境语言。 | | options.raw | boolean | 是 | false | 如果为 true,将返回从谷歌翻译 API 接收到的原始输出。 | | options.timeout | number | Yes | 30_000 | 翻译请求的超时持续时间(以毫秒为单位)。 | | options.retry | number | Yes | 0 | 如果失败,重试翻译请求的次数。 |

返回: Promise<TranslationOption>

Response Object:

| 键 | 类型 | 描述 | |----------------------------|------------------------------------------------|--------------------------------------------------| | text | string | 译文摘要,长文本将会被截断,完整结果请使用 to.text.value。 | | from | Record | - | | from.language | Record | - | | from.language.didYouMean | boolean | 表示是否有语言建议。 | | from.language.iso | string | 检测到的语言的 ISO 代码。 | | from.text | Record | - | | from.text.autoCorrected | boolean | 表示是否进行了自动更正。 | | from.text.value | string | 源文本。 | | from.text.phonetics | string | 源文本的音标。 | | from.text.didYouMean | boolean | 表示是否对源文本提出了建议。 | | from.synonyms | string[] | 源词的同义词。 | | from.sentences | string[] | 源词的示例句。 | | to | Record | - | | to.text | Record | - | | to.text.phonetics | string | 译文的音标。 | | to.text.value | string | 译文。 | | to.polysemy | Array<{ label: string; children: string[] }> | 译文的多义信息。 | | raw | string | 翻译请求的原始响应主体。仅在请求选项中的 options.rawtrue 时返回。 |


import { iso6391X } from '@kabeep/node-translate';

函数: getName(code)

iso6391X.getName(code);

| 参数 | 类型 | 可选 | 默认值 | 描述 | |--------|----------|----|-----|--------------------------------| | code | string | 否 | - | ISO-639-1 语言代码或 Google 翻译语言代码。 |

返回: string


函数: getAllNames()

iso6391X.getAllNames();

返回: string[]


函数: getNativeName(code)

iso6391X.getNativeName(code);

| 参数 | 类型 | 可选 | 默认值 | 描述 | |--------|----------|----|-----|--------------------------------| | code | string | 否 | - | ISO-639-1 语言代码或 Google 翻译语言代码。 |

返回: string


函数: getAllNativeNames()

iso6391X.getAllNativeNames();

返回: string[]


函数: getCode(name)

iso6391X.getCode(name);

| 参数 | 类型 | 可选 | 默认值 | 描述 | |--------|----------|----|-----|--------------------------------| | code | string | 否 | - | ISO-639-1 语言名称或 Google 翻译语言名称。 |

返回: LanguageCode


函数: getAllCodes()

iso6391X.getAllCodes();

返回: LanguageCode[]


函数: getLanguages(codes)

iso6391X.getLanguages(codes);

| 参数 | 类型 | 可选 | 默认值 | 描述 | |--------|----------|----|-----|------------------------------------| | code | string | 否 | - | ISO-639-1 语言代码元组或 Google 翻译语言代码元组。 |

返回: LanguageOption[]


函数: getAllDetections()

iso6391X.getAllDetections();

返回: LanguageCode[]

🪄 例子

使用语言代码

查看用例

import translate from '@kabeep/node-translate';

// 简单示例
translate('例子', { to: 'en' }).then(res => {
    // => example
    console.log(res.text);
});

使用语言名和大小写修正

查看用例

import translate from '@kabeep/node-translate';

// 使用语言名称并自动更正大小写
translate('例子', { to: 'ENGlish' }).then(res => {
    // => example
    console.log(res.text);
});

自适应翻译

查看用例

import translate from '@kabeep/node-translate';

// 使用 `auto` 或将 `from` 参数留空以通过适应性检测语言
// 使用 `auto` 或将 `to ` 参数留空以通过操作系统检测语言(示例环境使用 `en`)
translate('例子').then(res => {
    // => example
    console.log(res.to.text.value);
});

源文本与译本音标

查看用例

import translate from '@kabeep/node-translate';

// 输出源文本和译文的音标
translate('例子', { to: 'ja' }).then(res => {
    // => Lìzi
    console.log(res.from.text.phonetics);
    // => Rei
    console.log(res.to.text.phonetics);
});

源文本同、近义词

查看用例

import translate from '@kabeep/node-translate';

// 输出源词的同义词
translate('例子', { to: 'en' }).then(res => {
    // =>
    // [
    //     '例',
    //     '例子',
    //     '范例',
    //     '榜样',
    //     '典范',
    //     '例证',
    // ]
    console.log(res.from.synonyms);
});

源文本例句

查看用例

import translate from '@kabeep/node-translate';

// 输出源词的例句
translate('example', { to: 'zh' }).then(res => {
    // => [
    //     "it is vitally important that parents should set an [example]",
    //     "she followed her brother's [example] and deserted her family",
    //     "it's a good [example] of how European action can produce results",
    // ]
    console.log(res.from.sentences);
});

译本多义性

查看用例

import translate from '@kabeep/node-translate';

// 输出译文的多义信息
translate('例子', { to: 'en' }).then(res => {
    // => [{
    //     label: 'noun',
    //     children: [
    //         'example',
    //         'case',
    //         'instance',
    //     ],
    // }]
    console.log(res.to.polysemy);
});

源文本建议

查看用例

import translate from '@kabeep/node-translate';

// 自动检测并使用建议的正确源文本
translate('Thunk you', { from: 'en', to: 'zh' }).then(res => {
    // => 谢谢你
    console.log(res.to.text.value);
    // => true
    console.log(res.from.text.didYouMean);
});

自动修正源文本

查看用例

import translate from '@kabeep/node-translate';

// 自动纠正源文本中的拼写错误
translate('Thnak you', { from: 'en', to: 'zh' }).then(res => {
    // => 谢谢
    console.log(res.to.text.value);
    // => true
    console.log(res.from.text.autoCorrected);
});

自动修正源语言代码

查看用例

import translate from '@kabeep/node-translate';

// 自动检测并使用建议的正确源语言代码
translate('example', { from: 'zh', to: 'en' }).then(res => {
    // => en
    console.log(res.from.language.iso);
    // => true
    console.log(res.from.language.didYouMean);
});

// 自动检测并使用建议的正确源文本
translate('Thunk you', { from: 'en', to: 'zh' }).then(res => {
    // => 谢谢你
    console.log(res.to.text.value);
    // => true
    console.log(res.from.text.didYouMean);
});

网络异常重试

查看用例

import translate from '@kabeep/node-translate';

// 在翻译请求失败时进行重试尝试(此时最多三次请求)
translate('例子', { to: 'en', retry: 2, timeout: 100 }).catch((err) => {
    // => ETIMEDOUT - 达到了超时限制
    // => ECONNRESET - 连接被强制关闭
    // => EADDRINUSE - 无法绑定到任何空闲端口
    // => ECONNREFUSED - 服务器拒绝了连接
    // => EPIPE - 正在写入的流的远程端已关闭
    // => ENOTFOUND - 无法将主机名解析为 IP 地址
    // => ENETUNREACH - 没有网络连接
    // => EAI_AGAIN - DNS 查询超时
    // => EPARSE - 意料外的响应数据
    // => EVALIDATION - 非法语言代码
    console.log(err.message);
});

ISO-639-1-X

查看用例

import { iso6391X, LanguageCode, LanguageOption } from '@kabeep/node-translate';

// => en
console.log(iso6391X.getCode('english'));

// => ['aa', 'ab', ... 199 more items]
console.log(iso6391X.getAllCodes());

// => English
console.log(iso6391X.getName('en'));

// => ['Afar', 'Abkhaz', ... 199 more items]
console.log(iso6391X.getAllNames());

// => 中文
console.log(iso6391X.getNativeName('zh'));

// => ['Afaraf', 'аҧсуа бызшәа', ... 199 more items]
console.log(iso6391X.getAllNativeNames());

// => [
//   { code: 'en', name: 'English', nativeName: 'English' },
//   { code: 'zh', name: 'Chinese', nativeName: '中文' },
//   ... 6 more items
// ]
console.log(iso6391X.getAllDetections());

// => [
//   { code: 'en', name: 'English', nativeName: 'English' },
//   { code: 'zh', name: 'Chinese', nativeName: '中文' },
// ]
console.log(iso6391X.getLanguages(['en', 'zh']));

// => true
console.log(iso6391X.validate('en'));
// => false
console.log(iso6391X.validate('english'));

🔗 关联库

🤝 贡献

欢迎通过 Pull Requests 或 Issues 来贡献你的想法和代码。

📄 许可

本项目采用 MIT 许可证。详情请见 LICENSE 文件。