專為 JavaScript 前端打造的中文繁簡轉換庫
Chinese conversion library for JavaScript frontend
WebAssembly port of OpenCC with full API compatibility.
Bundles the official OpenCC C++ core compiled via Emscripten,
plus all official configs and prebuilt .ocd2 dictionaries.
OpenCC WASM 是 OpenCC 的 WebAssembly 移植版本,具有完整的 API 相容性。
內建透過 Emscripten 編譯的官方 OpenCC C++ 核心,以及所有官方與部分社區配置和預建的 .ocd2 字典檔案。
可在瀏覽器或 Node.js 環境中執行中文繁簡轉換,無需伺服器端處理。支援多種轉換模式:
OpenCC WASM is a WebAssembly port of OpenCC with full API compatibility.
Bundles the official OpenCC C++ core compiled via Emscripten, plus all official configs and
prebuilt .ocd2 dictionaries.
Enables Chinese Simplified-Traditional conversion directly in browsers or Node.js without server-side processing. Supports multiple conversion modes:
所有轉換在瀏覽器本地完成,無需網路請求,資料不會上傳。
All conversions run locally in your browser. No data is uploaded.
提供 opencc-js 相容 API,輕鬆遷移現有專案。
Compatible with opencc-js API for easy migration.
支援完整的 14 種 OpenCC 官方配置,包括地域用詞轉換。另有 4 種開源社區配置。
Full 14 OpenCC configs including regional phrase conversion. Also includes 4 additional configs from OpenCC open source community.
與原版 OpenCC 工具算法、功能完全一致,辭典格式完全兼容,並非仿寫。
Identical algorithm and functionality to the original OpenCC C++ tool, not a reimplementation
(unlike opencc-js which has some implementation errors).
與 wasm-opencc 相比,使用了真正的 WebAssembly 技術,兼容現代主流瀏覽器。
Uses genuine WebAssembly technology, compatible with all modern browsers.
兼容最新詞庫並致力於不斷修正,確保轉換品質與時俱進。
Compatible with the latest dictionaries and continuously improving conversion quality.
// 1. Import from CDN
import OpenCC from "https://cdn.jsdelivr.net/npm/opencc-wasm@0.6.1/dist/esm/index.js";
// 2. Create converter (auto-downloads everything!)
const converter = OpenCC.Converter({ config: "s2twp" });
// 3. Convert - Done!
const result = await converter("简体中文");
console.log(result); // 簡體中文
// Install: npm install opencc-wasm
import OpenCC from "opencc-wasm";
const converter = OpenCC.Converter({ config: "s2twp" });
const result = await converter("简体中文");
console.log(result); // 簡體中文
兩種指定轉換方式:
方式一:使用 config 參數(推薦)
// 簡體 → 台灣正體(含地域用詞)
const converter = OpenCC.Converter({ config: "s2twp" });
const result = await converter("服务器软件"); // 伺服器軟體
方式二:使用 from/to 參數(與 opencc-js 相容)
const converter = OpenCC.Converter({ from: "cn", to: "twp" });
const result = await converter("服务器"); // 伺服器
| Config | 說明 Description |
|---|---|
s2twp |
簡體 → 台灣正體(含地域用詞轉換) |
s2twp_jieba |
簡體 → 台灣正體(含地域用詞轉換,使用結巴分詞) |
s2tw |
簡體 → 台灣正體 |
s2hk |
簡體 → 香港繁體 |
s2t |
簡體 → OpenCC 標準繁體 |
tw2s |
台灣正體 → 簡體 |
tw2sp |
台灣正體 → 簡體(含地域用詞轉換) |
tw2sp_jieba |
台灣正體 → 簡體(含地域用詞轉換,使用結巴分詞) |
hk2s |
香港繁體 → 簡體 |
t2s |
OpenCC 標準繁體 → 簡體 |
hk2t / t2hk |
香港繁體 ↔ OpenCC 標準繁體 |
tw2t / t2tw |
台灣正體 ↔ OpenCC 標準繁體 |
jp2t / t2jp |
日文新字體 ↔ 舊字體 |
t2cngov |
規範化為「通用規範漢字表」標準繁體 (來自TerryTian-Tech) 試用 |
t2cngov_keep_simp |
僅規範化繁體字為「通用規範漢字表」標準繁體,簡體字保持不變 (來自TerryTian-Tech) 試用 |
const converter = OpenCC.CustomConverter([
["“", "「"],
["”", "」"],
["‘", "『"],
["’", "』"],
]);
const result = converter("这是“引号”和‘单引号’");
// Output: 这是「引号」和『单引号』
import { useState } from 'react';
import OpenCC from 'opencc-wasm';
function App() {
const [output, setOutput] = useState('');
const handleConvert = async () => {
const converter = OpenCC.Converter({ config: "s2tw" });
setOutput(await converter("简体中文"));
};
return (
<div>
<button onClick={handleConvert}>Convert</button>
<div>{output}</div>
</div>
);
}
<script setup>
import { ref } from 'vue';
import OpenCC from 'opencc-wasm';
const output = ref('');
async function handleConvert() {
const converter = OpenCC.Converter({ config: "s2tw" });
output.value = await converter("简体中文");
}
</script>
<template>
<button @click="handleConvert">Convert</button>
<div>{{ output }}</div>
</template>
✅ 重複使用轉換器實例
// ✅ Good: Create once, use many times
const converter = OpenCC.Converter({ config: "s2tw" });
for (const text of manyTexts) {
await converter(text); // Fast!
}
// ❌ Avoid: Creating new instances every time
for (const text of manyTexts) {
const converter = OpenCC.Converter({ config: "s2tw" }); // Slow!
await converter(text);
}
多個轉換器(自動快取)
// Create multiple converters (resources auto-cached)
const s2t = OpenCC.Converter({ config: "s2t" });
const s2tw = OpenCC.Converter({ config: "s2tw" });
const t2s = OpenCC.Converter({ config: "t2s" });
// Use independently
console.log(await s2t("简体")); // 簡體
console.log(await s2tw("软件")); // 軟體
console.log(await t2s("繁體")); // 繁体