Han(汉)
a module for tanslating Chinese(汉字) into pinyin.
Installation
$ npm install han
Usage
1. han.letter()
han.letter(chinese ,[separator ,[callback(err, data)])
Use the letter
method to translate Chinese in to english letter:
var han = require('han');
han.letter('中文')
han.letter('中文', '-')
han.letter('中文', function(err, result){
console.log(result)
})
The letter
method has 3 params by default, they can all be optional, it depends on you(^^). follow the message below, you can also find it at lib/translate.js:
2. han.pinyin(chinese)
User the pinyin
method to translate Chinese into Pinyin(letters with notation):
han.pinyin('中文')
han.pinyin('My Chinese name is 小鱼(sofish)');
WHY the return value is an array?
Beause that a Chinese word always having more than one pronunciation. You may want to allow users to correct your output that is generated by your program in an automatic way(like google?! yep!).
Testcase
$ npm install han
$ cd node_modules/han && node testcase.js
Translating Messy Code
Messy code? orginal file is here testcase.js:
var han = require('han');
console.log('orginal:要实现 Speaker Deck 那种中文转拼音的')
console.log(han.letter('要实现 Speaker Deck 那种中文转拼音的', '-'), '\n')
han.letter('中aaaaa中¢∞§¶•誩aa文喳aa', function(err, result){
if(err) throw err;
console.log('orginal:中aaaaa中¢∞§¶•誩aa文喳aa')
console.log('callback:' + result, '\n')
});
han.letter('中EnglishWords¢∞§¶•ªº文', '-', function(err, result){
console.log('orginal:中EnglishWords¢∞§¶•ªº文')
console.log('callback:' + result)
});
console.log('original: My Chinese name is 小鱼(sofish)')
console.log(han.pinyin('My Chinese name is 小鱼(sofish)'))
console.log('original: #$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有英文:english')
console.log(han.pinyin('#$%^&*中23¢∞§¶•ª52849文@#$%^&*(意思,还有英文:english'))
License
Licensed under MIT.
Contributers:
Specail thank to fayland for providing the unicode table of Chinese.