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 🙏

© 2024 – Pkg Stats / Ryan Hefner

baidut

v0.2.1

Published

Baidut, translator for Baidu for both NodeJS and Browser

Downloads

27

Readme

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.