| 简体中文 | 繁體中文 | English |
简繁体中文转换库 - 支持简体中文与繁体中文双向转换,基于词组的智能分词处理「一简多繁」问题
使用 npm 安装:
npm install switch-chinese
使用 yarn 安装:
yarn add switch-chinese
import stcasc from 'switch-chinese';
const { traditionalized, simplized, detect } = stcasc();
// 简体转繁体
const tc = traditionalized('简繁转换 繁简切换 香烟 香烟袅袅');
console.log(tc);
// 输出: 簡繁轉換 繁簡切換 香菸 香煙裊裊
// 繁体转简体
const sc = simplized('繁體中文');
console.log(sc);
// 输出: 繁体中文
import stcasc, { ChineseType } from 'switch-chinese';
const { detect } = stcasc();
const type1 = detect('简体中文');
if (type1 === ChineseType.SIMPLIFIED) {
console.log('检测到简体中文');
}
const type2 = detect('繁體中文');
if (type2 === ChineseType.TRADITIONAL) {
console.log('检测到繁体中文');
}
const type3 = detect('English');
if (type3 === ChineseType.UNKNOWN) {
console.log('未检测到中文');
}
ChineseType 枚举值:
ChineseType.SIMPLIFIED (0): 简体中文ChineseType.TRADITIONAL (1): 繁体中文ChineseType.UNKNOWN (2): 未知类型在多次调用时,建议使用缓存机制避免重复生成字典,提升性能:
import stcasc from 'switch-chinese';
// 第一次调用,生成字典
let converter = stcasc();
const cache = converter.cache;
// 保存缓存到持久化存储(如 localStorage、文件等)
localStorage.setItem('stcasc-cache', JSON.stringify(cache));
// 后续调用,直接使用缓存
const cachedData = JSON.parse(localStorage.getItem('stcasc-cache'));
converter = stcasc(cachedData);
// 现在可以直接使用,无需重新生成字典
const result = converter.traditionalized('简体中文');
可以根据业务需求自定义简繁转换规则:
import stcasc from 'switch-chinese';
const customDict = {
'身份': '身分',
'转义': '跳脫',
'转换': '轉檔',
'软件': '軟體',
'硬件': '硬體',
'网络': '網路',
'服务器': '伺服器'
};
const { traditionalized, simplized } = stcasc({}, customDict);
console.log(traditionalized('软件转换'));
// 输出: 軟體轉檔(使用自定义词库)
默认情况下,库会转换一些特定术语(如「知识产权」→「智慧財產權」)。如需禁用此功能:
import stcasc from 'switch-chinese';
// 第三个参数设置为 true 以禁用术语转换
const { traditionalized } = stcasc({}, {}, true);
console.log(traditionalized('知识产权'));
// 输出: 知識産權(仅做字符转换,不转换术语)
库支持多种输出格式:
import stcasc, { OutputFormat } from 'switch-chinese';
const { traditionalized } = stcasc();
// 普通格式(默认)
const normal = traditionalized('简体中文');
// 输出: 簡體中文
// 括号格式:同时显示原文和转换后的文本
const bracket = traditionalized('简体中文', { format: OutputFormat.BRACKET });
// 输出: 簡(简)體(体)中文
// Ruby 注音格式:适用于 HTML 显示
const ruby = traditionalized('简体中文', { format: OutputFormat.RUBY });
// 输出: <ruby>簡<rt>简</rt></ruby><ruby>體<rt>体</rt></ruby>中文
OutputFormat 枚举值:
OutputFormat.NORMAL (0): 只输出转换后的结果(默认)OutputFormat.BRACKET (1): 输出「转换(原文)」格式OutputFormat.RUBY (2): 输出 <ruby> HTML 标签格式主函数,用于创建转换器实例。
参数:
cache (Object, 可选): 缓存对象,用于避免重复生成字典custom (Object, 可选): 自定义简繁转换词库disableTerms (Boolean, 可选): 是否禁用术语转换,默认 false返回值:
返回包含以下方法的对象:
traditionalized(text, options?): 将简体中文转换为繁体中文simplized(text, options?): 将繁体中文转换为简体中文detect(text): 检测文本的中文类型,返回 ChineseType 枚举值cache: 字典缓存对象Options 参数:
format (Number, 可选): 输出格式,使用 OutputFormat 常量导出的常量对象,用于表示中文类型检测结果:
export const ChineseType = {
SIMPLIFIED: 0, // 简体中文
TRADITIONAL: 1, // 繁体中文
UNKNOWN: 2 // 未知类型
};
导出的常量对象,用于表示输出格式选项:
export const OutputFormat = {
NORMAL: 0, // 只输出转换后的结果
BRACKET: 1, // 输出「转换(原文)」格式
RUBY: 2 // 输出 <ruby> HTML 标签格式
};
该库支持基于上下文的智能词组转换,能够正确处理「一简多繁」的情况:
const { traditionalized } = stcasc();
// 智能识别词组边界
console.log(traditionalized('香烟袅袅'));
// 输出: 香煙裊裊
console.log(traditionalized('里长面子'));
// 输出: 里長面子(「里长」是职务名称)
console.log(traditionalized('吃干面'));
// 输出: 吃乾麵(「干面」是食物)
console.log(traditionalized('把考卷发回来'));
// 输出: 把考卷發回來(「发」是动词)
console.log(traditionalized('卷发'));
// 输出: 捲髮(「卷发」是发型)
内置常用的大陆简体与台湾正体术语转换:
const { traditionalized } = stcasc();
console.log(traditionalized('知识产权'));
// 输出: 智慧財產權
console.log(traditionalized('计算机软件'));
// 输出: 計算機軟體
console.log(traditionalized('网络服务器'));
// 输出: 網路伺服器
支持所有现代浏览器及 Node.js 环境:
MIT License
简繁转换, 繁简转换, 简体中文, 繁体中文, 正体中文, 中文转换, 一简多繁, 简繁切换, 繁简切换, 中文检测, 智能分词, 自定义词库, 零依赖, 轻量级
Lightweight Chinese converter library for bidirectional conversion between Simplified and Traditional Chinese with intelligent word segmentation and one-to-many character mapping support.
Install via npm:
npm install switch-chinese
Install via yarn:
yarn add switch-chinese
import stcasc from 'switch-chinese';
const { traditionalized, simplized, detect } = stcasc();
// Simplified to Traditional
const tc = traditionalized('简繁转换 繁简切换 香烟 香烟袅袅');
console.log(tc);
// Output: 簡繁轉換 繁簡切換 香菸 香煙裊裊
// Traditional to Simplified
const sc = simplized('繁體中文');
console.log(sc);
// Output: 繁体中文
import stcasc, { ChineseType } from 'switch-chinese';
const { detect } = stcasc();
const type1 = detect('简体中文');
if (type1 === ChineseType.SIMPLIFIED) {
console.log('Detected Simplified Chinese');
}
const type2 = detect('繁體中文');
if (type2 === ChineseType.TRADITIONAL) {
console.log('Detected Traditional Chinese');
}
const type3 = detect('English');
if (type3 === ChineseType.UNKNOWN) {
console.log('No Chinese text detected');
}
ChineseType enumeration values:
ChineseType.SIMPLIFIED (0): Simplified ChineseChineseType.TRADITIONAL (1): Traditional ChineseChineseType.UNKNOWN (2): Unknown typeFor multiple conversions, use caching mechanism to improve performance:
import stcasc from 'switch-chinese';
// First call, generate dictionary
let converter = stcasc();
const cache = converter.cache;
// Save cache to persistent storage (e.g., localStorage, file system)
localStorage.setItem('stcasc-cache', JSON.stringify(cache));
// Subsequent calls, use cached data
const cachedData = JSON.parse(localStorage.getItem('stcasc-cache'));
converter = stcasc(cachedData);
// Now you can use it directly without regenerating dictionary
const result = converter.traditionalized('简体中文');
Customize conversion rules according to your needs:
import stcasc from 'switch-chinese';
const customDict = {
'身份': '身分',
'转义': '跳脫',
'转换': '轉檔',
'软件': '軟體',
'硬件': '硬體',
'网络': '網路',
'服务器': '伺服器'
};
const { traditionalized, simplized } = stcasc({}, customDict);
console.log(traditionalized('软件转换'));
// Output: 軟體轉檔 (using custom dictionary)
By default, the library converts specific terms (e.g., “知识产权” → “智慧財產權”). To disable this feature:
import stcasc from 'switch-chinese';
// Set the third parameter to true to disable term conversion
const { traditionalized } = stcasc({}, {}, true);
console.log(traditionalized('知识产权'));
// Output: 知識産權 (character conversion only, no term conversion)
The library supports multiple output formats:
import stcasc, { OutputFormat } from 'switch-chinese';
const { traditionalized } = stcasc();
// Normal format (default)
const normal = traditionalized('简体中文');
// Output: 簡體中文
// Bracket format: shows both original and converted text
const bracket = traditionalized('简体中文', { format: OutputFormat.BRACKET });
// Output: 簡(简)體(体)中文
// Ruby annotation format: for HTML display
const ruby = traditionalized('简体中文', { format: OutputFormat.RUBY });
// Output: <ruby>簡<rt>简</rt></ruby><ruby>體<rt>体</rt></ruby>中文
OutputFormat enumeration values:
OutputFormat.NORMAL (0): Output converted result only (default)OutputFormat.BRACKET (1): Output in “converted(original)” formatOutputFormat.RUBY (2): Output in <ruby> HTML tag formatMain function to create a converter instance.
Parameters:
cache (Object, optional): Cache object to avoid regenerating dictionarycustom (Object, optional): Custom Simplified-Traditional conversion dictionarydisableTerms (Boolean, optional): Whether to disable term conversion, default falseReturns:
An object containing the following methods:
traditionalized(text, options?): Convert Simplified Chinese to Traditional Chinesesimplized(text, options?): Convert Traditional Chinese to Simplified Chinesedetect(text): Detect Chinese text type, returns ChineseType enumeration valuecache: Dictionary cache objectOptions parameter:
format (Number, optional): Output format, use OutputFormat constantsExported constant object representing text detection results:
export const ChineseType = {
SIMPLIFIED: 0, // Simplified Chinese
TRADITIONAL: 1, // Traditional Chinese
UNKNOWN: 2 // Unknown type
};
Exported constant object representing output format options:
export const OutputFormat = {
NORMAL: 0, // Output converted result only
BRACKET: 1, // Output "converted(original)" format
RUBY: 2 // Output <ruby> HTML tag format
};
The library supports context-aware intelligent word segmentation, accurately handling one-to-many character mappings:
const { traditionalized } = stcasc();
// Intelligent phrase boundary recognition
console.log(traditionalized('香烟袅袅'));
// Output: 香煙裊裊
console.log(traditionalized('里长面子'));
// Output: 里長面子 ("里长" is a position title)
console.log(traditionalized('吃干面'));
// Output: 吃乾麵 ("干面" is a food)
console.log(traditionalized('把考卷发回来'));
// Output: 把考卷發回來 ("发" is a verb in this context)
console.log(traditionalized('卷发'));
// Output: 捲髮 ("卷发" is a hairstyle)
Built-in conversion for common mainland China and Taiwan terminology:
const { traditionalized } = stcasc();
console.log(traditionalized('知识产权'));
// Output: 智慧財產權
console.log(traditionalized('计算机软件'));
// Output: 計算機軟體
console.log(traditionalized('网络服务器'));
// Output: 網路伺服器
Supports all modern browsers and Node.js environments:
MIT License
Chinese Converter, Simplified Chinese, Traditional Chinese, Chinese Translation, Character Detection, Text Converter, Language Converter, i18n, Localization, Ruby Annotation, One-to-Many Mapping, Intelligent Word Segmentation, Custom Dictionary, Zero Dependencies, Lightweight
簡繁體中文智能轉換庫 - 支援簡體中文與正體中文雙向轉換,基於詞組的智能分詞處理「一簡多繁」問題
使用 npm 安裝:
npm install switch-chinese
使用 yarn 安裝:
yarn add switch-chinese
import stcasc from 'switch-chinese';
const { traditionalized, simplized, detect } = stcasc();
// 簡體轉繁體
const tc = traditionalized('简繁转换 繁简切换 香烟 香烟袅袅');
console.log(tc);
// 輸出: 簡繁轉換 繁簡切換 香菸 香煙裊裊
// 繁體轉簡體
const sc = simplized('繁體中文');
console.log(sc);
// 輸出: 繁体中文
import stcasc, { ChineseType } from 'switch-chinese';
const { detect } = stcasc();
const type1 = detect('简体中文');
if (type1 === ChineseType.SIMPLIFIED) {
console.log('檢測到簡體中文');
}
const type2 = detect('繁體中文');
if (type2 === ChineseType.TRADITIONAL) {
console.log('檢測到繁體中文');
}
const type3 = detect('English');
if (type3 === ChineseType.UNKNOWN) {
console.log('未檢測到中文');
}
ChineseType 列舉值:
ChineseType.SIMPLIFIED (0): 簡體中文ChineseType.TRADITIONAL (1): 繁體中文ChineseType.UNKNOWN (2): 未知類型在多次呼叫時,建議使用快取機制避免重複生成字典,提升效能:
import stcasc from 'switch-chinese';
// 第一次呼叫,生成字典
let converter = stcasc();
const cache = converter.cache;
// 將快取儲存至持久化儲存(如 localStorage、檔案等)
localStorage.setItem('stcasc-cache', JSON.stringify(cache));
// 後續呼叫,直接使用快取
const cachedData = JSON.parse(localStorage.getItem('stcasc-cache'));
converter = stcasc(cachedData);
// 現在可以直接使用,無需重新生成字典
const result = converter.traditionalized('简体中文');
可以根據業務需求自訂簡繁轉換規則:
import stcasc from 'switch-chinese';
const customDict = {
'身份': '身分',
'转义': '跳脫',
'转换': '轉檔',
'软件': '軟體',
'硬件': '硬體',
'网络': '網路',
'服务器': '伺服器'
};
const { traditionalized, simplized } = stcasc({}, customDict);
console.log(traditionalized('软件转换'));
// 輸出: 軟體轉檔(使用自訂詞庫)
預設情況下,函式庫會轉換一些特定術語(如「知识产权」→「智慧財產權」)。如需停用此功能:
import stcasc from 'switch-chinese';
// 第三個參數設定為 true 以停用術語轉換
const { traditionalized } = stcasc({}, {}, true);
console.log(traditionalized('知识产权'));
// 輸出: 知識産權(僅做字元轉換,不轉換術語)
函式庫支援多種輸出格式:
import stcasc, { OutputFormat } from 'switch-chinese';
const { traditionalized } = stcasc();
// 普通格式(預設)
const normal = traditionalized('简体中文');
// 輸出: 簡體中文
// 括號格式:同時顯示原文和轉換後的文字
const bracket = traditionalized('简体中文', { format: OutputFormat.BRACKET });
// 輸出: 簡(简)體(体)中文
// Ruby 注音格式:適用於 HTML 顯示
const ruby = traditionalized('简体中文', { format: OutputFormat.RUBY });
// 輸出: <ruby>簡<rt>简</rt></ruby><ruby>體<rt>体</rt></ruby>中文
OutputFormat 列舉值:
OutputFormat.NORMAL (0): 只輸出轉換後的結果(預設)OutputFormat.BRACKET (1): 輸出「轉換(原文)」格式OutputFormat.RUBY (2): 輸出 <ruby> HTML 標籤格式主函式,用於建立轉換器實例。
參數:
cache (Object, 可選): 快取物件,用於避免重複生成字典custom (Object, 可選): 自訂簡繁轉換詞庫disableTerms (Boolean, 可選): 是否停用術語轉換,預設 false回傳值:
回傳包含以下方法的物件:
traditionalized(text, options?): 將簡體中文轉換為繁體中文simplized(text, options?): 將繁體中文轉換為簡體中文detect(text): 檢測文字的中文類型,回傳 ChineseType 列舉值cache: 字典快取物件Options 參數:
format (Number, 可選): 輸出格式,使用 OutputFormat 常數匯出的常數物件,用於表示中文類型檢測結果:
export const ChineseType = {
SIMPLIFIED: 0, // 簡體中文
TRADITIONAL: 1, // 繁體中文
UNKNOWN: 2 // 未知類型
};
匯出的常數物件,用於表示輸出格式選項:
export const OutputFormat = {
NORMAL: 0, // 只輸出轉換後的結果
BRACKET: 1, // 輸出「轉換(原文)」格式
RUBY: 2 // 輸出 <ruby> HTML 標籤格式
};
本函式庫支援基於上下文的智能詞組轉換,能夠正確處理「一簡多繁」的情況:
const { traditionalized } = stcasc();
// 智能識別詞組邊界
console.log(traditionalized('香烟袅袅'));
// 輸出: 香煙裊裊
console.log(traditionalized('里长面子'));
// 輸出: 里長面子(「里長」是職務名稱)
console.log(traditionalized('吃干面'));
// 輸出: 吃乾麵(「乾麵」是食物)
console.log(traditionalized('把考卷发回来'));
// 輸出: 把考卷發回來(「發」是動詞)
console.log(traditionalized('卷发'));
// 輸出: 捲髮(「捲髮」是髮型)
內建常用的大陸簡體與台灣正體術語轉換:
const { traditionalized } = stcasc();
console.log(traditionalized('知识产权'));
// 輸出: 智慧財產權
console.log(traditionalized('计算机软件'));
// 輸出: 計算機軟體
console.log(traditionalized('网络服务器'));
// 輸出: 網路伺服器
支援所有現代瀏覽器及 Node.js 環境:
MIT License
簡繁轉換, 繁簡轉換, 簡體中文, 繁體中文, 正體中文, 中文轉換, 一簡多繁, 簡繁切換, 繁簡切換, 中文檢測, 智能分詞, 自訂詞庫, 零依賴, 輕量級