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

chinese-simple2traditional

v2.2.2

Published

中文繁简体互换,支持node端、浏览器端。

Downloads

419

Readme

中文繁简体转换

jsr jsr score

NPM Version NPM Downloads npm package minimized gzipped size NPM License GitHub issue custom search

  • 内置字库,零依赖,支持任何运行环境。
  • 支持 3077+ 常用简体字 和 4919+ 繁体字/异体字。
  • 支持 3577+ 简体短语转繁体的特殊情况。
  • 支持 117+ 繁体短语/异体短语 转简体的特殊情况。

内置字库 < 30kb,短语库 约 374kb。为了避免加载资源过多,你需要手动加载短语库。

v2 相比于 v1 版本,提供了更多的字库支持!更精确的繁简体转换!更少的转换耗时!

[!IMPORTANT] v2 版本仅支持 ESM 导入,不再支持 CommonJS 和 IIFE。

如果你想使用 CommonJS 或 IIFE 导入,请使用 v1 版本。

预览

访问 在线演示 查看。

# npm
npm install chinese-simple2traditional
# yarn
yarn add chinese-simple2traditional
# pnpm
pnpm add chinese-simple2traditional

也可以使用 JSR ,但包名重命名为 @raise/han-convert

# npm
npx jsr add @raise/han-convert
# yarn
yarn dlx jsr add @raise/han-convert
# pnpm
pnpm dlx jsr add @raise/han-convert
# bun
bunx jsr add @raise/han-convert
# deno
deno add @raise/han-convert

Usage

import { toSimplified, toTraditional } from 'chinese-simple2traditional'

toTraditional('主人何为言少钱,径须酤取对君酌。') // 主人何爲言少錢,徑須酤取對君酌

toSimplified('主人何爲言少錢,徑須酤取對君酌') // 主人何为言少钱,径须酤取对君酌。

默认情况下,繁简体转换仅是逐个对字符进行处理,因此获取的结果可能并不是精确的。 优势是加载的包体积 <30kb

你还可以额外引入 短语库,获取更为精确的转换结果,但需要额外加载 374kb+ 的短语库。

import { toSimplified, toTraditional } from 'chinese-simple2traditional'
import { setupEnhance } from 'chinese-simple2traditional/enhance'

setupEnhance() // 注入短语库

// 默认情况下,王后 被错误转为 王後
toSimplified('王妃后来成为了王后') // 王妃後來成爲了王後
// 传入第二个参数 `true` 以获取精确结果
toSimplified('王妃后来成为了王后', true) // 王妃後來成爲了王后

如果是使用 JSR 安装,请使用以下语句导入:

import { toSimplified, toTraditional } from '@raise/han-convert'
import { setupEnhance } from '@raise/han-convert/enhance'

API

toSimplified(text[, enhance])

  • text: string, 待转换的文本
  • enhance: boolean, 是否启用短语库,只有在 注入短语库 后有效

将文本简体转换为繁体。

toTraditional(text[, enhance])

  • text: string, 待转换的文本
  • enhance: boolean, 是否启用短语库,只有在 注入短语库 后有效

将文本繁体转换为简体。

setupEnhance()

注入短语库。

import { setupEnhance } from 'chinese-simple2traditional/enhance'
// or JSR
import { setupEnhance } from '@raise/han-convert/enhance'

customS2TPhrases(phrases)

添加 自定义 简体转繁体 短语集合。

  • phrases: [string, string][] , 短语集合

    集合的每个元素为 ['简体短语', '繁体短语'] 的元组,表示 简体短语 转换为 繁体短语

import { customS2TPhrases, toTraditional } from 'chinese-simple2traditional'

customS2TPhrases([
  ['双台子区', '雙臺子區'],
  // ...
])

toTraditional('双台子区', true) // 雙臺子區

customT2SPhrases(phrases)

添加 自定义 繁体转简体 短语集合。

  • phrases: [string, string][] , 短语集合

    集合的每个元素为 ['繁体短语', '简体短语'] 的元组, 表示 繁体短语 转换为 简体短语

import { customT2SPhrases, toSimplified } from 'chinese-simple2traditional'

customT2SPhrases([
  ['雖覆能復', '虽覆能复']
  // ...
])

toSimplified('雖覆能復', true) // 虽覆能复

LICENSE

MIT