• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iamtraction/google-translate:

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

iamtraction/google-translate

开源软件地址(OpenSource Url):

https://github.com/iamtraction/google-translate

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

Google Translate API

A Node.JS library to consume Google Translate for free.

GitHub release Dependencies Known Vulnerabilities license

Feature Highlights

  • Automatically detect source language
  • Automatic spelling corrections
  • Automatic language correction
  • Fast and reliable

Table of Contents

Installation

# Stable version, from npm repository
npm install --save @iamtraction/google-translate

# Latest version, from GitHub repository
npm install --save iamtraction/google-translate

Usage

// If you've installed from npm, do:
const translate = require('@iamtraction/google-translate');

// If you've installed from GitHub, do:
const translate = require('google-translate');

Method: translate(text, options)

translate(text, options).then(console.log).catch(console.error);
Parameter Type Optional Default Description
text String No - The text you want to translate.
options Object - - The options for translating.
options.from String Yes 'auto' The language name/ISO 639-1 code to translate from. If none is given, it will auto detect the source language.
options.to String Yes 'en' The language name/ISO 639-1 code to translate to. If none is given, it will translate to English.
options.raw Boolean Yes false If true, it will return the raw output that was received from Google Translate.

Returns: Promise<Object>

Response Object:

Key Type Description
text String The translated text.
from Object -
from.language Object -
from.language.didYouMean Boolean Whether or not the API suggest a correction in the source language.
from.language.iso String The ISO 639-1 code of the language that the API has recognized in the text.
from.text Object -
from.text.autoCorrected Boolean Whether or not the API has auto corrected the original text.
from.text.value String The auto corrected text or the text with suggested corrections. Only returned if from.text.autoCorrected or from.text.didYouMean is true.
from.text.didYouMean Boolean Wherether or not the API has suggested corrections to the text
raw String The raw response from Google Translate servers. Only returned if options.raw is true in the request options.

Examples

From automatic language detection to English:

translate('Tu es incroyable!', { to: 'en' }).then(res => {
  console.log(res.text); // OUTPUT: You are amazing!
}).catch(err => {
  console.error(err);
});

From English to French, with a typo:

translate('Thank you', { from: 'en', to: 'fr' }).then(res => {
  console.log(res.text); // OUTPUT: Je vous remercie
  console.log(res.from.autoCorrected); // OUTPUT: true
  console.log(res.from.text.value); // OUTPUT: [Thank] you
  console.log(res.from.text.didYouMean); // OUTPUT: false
}).catch(err => {
  console.error(err);
});

Sometimes Google Translate won't auto correct:

translate('Thank you', { from: 'en', to: 'fr' }).then(res => {
  console.log(res.text); // OUTPUT: ''
  console.log(res.from.autoCorrected); // OUTPUT: false
  console.log(res.from.text.value); // OUTPUT: [Thank] you
  console.log(res.from.text.didYouMean); // OUTPUT: true
}).catch(err => {
  console.error(err);
});

Extras

If you liked this project, please give it a in GitHub.

Credits to matheuss for writing the original version of this library. I rewrote this, with improvements and without using many external libraries, as his library was not actively developed and had vulnerabilities.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
geohackweek/GoogleEarthEngine: Google Earth Engine Tutorial发布时间:2022-06-11
下一篇:
Harmon758/Google-Kick-Start: Google Kick Start发布时间:2022-06-11
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap