Socket
Socket
Sign inDemoInstall

baidut

Package Overview
Dependencies
0
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    baidut

Baidut, translator for Baidu for both NodeJS and Browser


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

baidut

Baidu translator for NodeJS and Browser with Promise ready.

On browser, it will make a new request via JSONP as Baidu doesn't support CORS. It offers option to be set either to request for Baidu's HTTP, or HTTPS URL end-point.

On NodeJS, it will make a new request via normal NodeJS's http|https module with options to be set either to go with GET or POST request, and either request for Baidu's HTTP, or HTTPS URL end-point.

Installation

Install it via npm install baidut.

For browser, you can execute npm run build to create distribution fies in ./dist directory.
Then inside your .html file of your project, you can include it via

<script src="dist/Baidut.min.js"></script>

Usage

NodeJS

Include it in your project by

var Baidut = require('Baidut');

Prepare Baidu environment first before making a call to translate.

Baidut.builder
    .setAppId("2015063000000001")
    .setKey("12345678")
    .build();

appId=2015063000000001, and key=12345678 is working app id and key for Baidu's demo app. You can test using them first.

Then translate it via [default to translate from Chinese to English]

Baidut.translate('苹果')
  .then((result) => {
  	console.log("translated text: " + result.trans_result[0].dst);
  }, (e) => {
  	console.log(e.code + ": " + e.message);
  });

Above will translate input text from Chinese to English with POST method of HTTPS request on NodeJS. For browser, it goes with GET method with HTTPS request via JSONP on browser.

You set options via

Baidut.translate('สวัสดี', { http_request: true, get_method: true, from_lang: "th", to_lang: "en" })
	.then((result) => {
		console.log("translated text: " + result.trans_result[0].dst);
	}, (e) => {
		console.log(e.code + ": " + e.message);
	});

Above set http_request=true to force it to make a request via HTTP protocol, get_method=true only available on NodeJS to force it to use GET method for request, and set from_lang="th" to mark that the source langauge is Thai, and to_lang="en" to mark the result we want in English.

See list of language support at official document.

Set Charset on Browser

As you likely to involve translating Unicode language as well thus defining that your HTML file has UTF-16 encoding is important to have conistency in displaying proper text for non-English language.

Please note that HTML4 support only UTF-8, but HTML5 support both UTF-8 and UTF-16.

<head>
...
<meta charset="UTF-16">
...
</head>

Developers

There are commands available for development, build documents, build distribution files, and others.

  • npm run build - to build a distribution files (included es5-dev, es6, es2015, and minfied version)
  • npm run docs - to generate document in ./docs directory. Not that it uses JSDoc from comment in code.
  • npm test - to run tests for both NodeJS and Browser.

Document

License

MIT. See and understand license here.

Keywords

FAQs

Last updated on 06 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc