A pure JavaScript lib to convert hanzi (Chinese) to pinyin with Intl API.
The lib takes advantage of Intl.Collator
API, and can convert hanzi to pinyin with less than 500 lines (include built-in small dict)! And the lib can be used with all browsers who support Intl.Collator
API and support locale languages.
Features:
- The lib is about 300 lines, very lightweight.
- Correctly process 6763 common hanzi, as well as other hanzi.
- Both support node.js and browser.
Install via npm: $ npm i tiny-pinyin --save
.
Usage:
// test.js
const pinyin = require('tiny-pinyin')
if (pinyin.isSupported()) {
pinyin.convertToPinyin('我') // WO
}
Note that the charset of the page must be utf-8 when used in a browser, see issue#21.
node.js
default support English and if we want it support chinese(zh-Hans-CN
), install full-icu
:
npm i --save full-icu
full-icu
will automatically install missing ICU
data file, and then we can run node --icu-data-dir=node_modules/full-icu test.js
to execute out code successfully.
More info with full-icu-npm & Node Intl.
forceDetect
, type isBoolean
, set whether force re-detect.
Test whether env supoort Intl.Collator
and zh-CN
.
string
, type isString
Return token list converted from specified string. Typical token:
{
type: Number, // 1-LATIN, 2-Pinyin, 3-Unknown
source: String, // source
target: String // converted pinyin
}
string
, type isString
.separator
, type isString
.lowerCase
, type isBoolean
. Only work for pinyin.
Return converted pinyin string.
pinyin.convertToPinyin('我们a', null, false) // 'WOMENa'
pinyin.convertToPinyin('我们a', '-', true) // 'wo-men-a'
Inspired by Android Contacts Source Code, and Thanks for their efforts.