OpenCC WASM

更新日期:2026-01-08

OpenCC WASM

專為 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 WASMOpenCC 的 WebAssembly 移植版本,具有完整的 API 相容性。 內建透過 Emscripten 編譯的官方 OpenCC C++ 核心,以及所有官方與部分社區配置和預建的 .ocd2 字典檔案。

可在瀏覽器或 Node.js 環境中執行中文繁簡轉換,無需伺服器端處理。支援多種轉換模式:

  • 簡體中文 ↔ 繁體中文(標準、台灣、香港)
  • 地域用詞轉換(如「軟體」↔「软件」)
  • 日文新字體 ↔ 舊字體
  • 通用規範漢字表轉換

EN About OpenCC WASM

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:

  • Simplified ↔ Traditional Chinese (Standard, Taiwan, Hong Kong)
  • Regional phrase conversion (e.g., "軟體" ↔ "软件")
  • Japanese Shinjitai ↔ Kyūjitai
  • China's General Standard Characters conversion

功能特色 Features

🌐 純前端運行 Client-Side Only

所有轉換在瀏覽器本地完成,無需網路請求,資料不會上傳。
All conversions run locally in your browser. No data is uploaded.

📦 易於整合 Easy Integration

提供 opencc-js 相容 API,輕鬆遷移現有專案。
Compatible with opencc-js API for easy migration.

🔧 完整 16 種配置 16 Configurations

支援完整的 OpenCC 官方配置,包括地域用詞轉換。另有部分開源社區配置。
Full OpenCC configs including regional phrase conversion. Some configs are from OpenCC open source community.

✅ 原版算法 Authentic Algorithm

與原版 OpenCC 工具算法、功能完全一致,辭典格式完全兼容,並非仿寫。
Identical algorithm and functionality to the original OpenCC C++ tool, not a reimplementation (unlike opencc-js which has some implementation errors).

🚀 真正的 WASM Real WebAssembly

與 wasm-opencc 相比,使用了真正的 WebAssembly 技術,兼容現代主流瀏覽器。
Uses genuine WebAssembly technology, compatible with all modern browsers.

📚 持續更新 Continuously Updated

兼容最新詞庫並致力於不斷修正,確保轉換品質與時俱進。
Compatible with the latest dictionaries and continuously improving conversion quality.

📖 使用方法 Usage

🌐 Browser (CDN)
// 1. Import from CDN
import OpenCC from "https://cdn.jsdelivr.net/npm/opencc-wasm@0.4.1/dist/esm/index.js";

// 2. Create converter (auto-downloads everything!)
const converter = OpenCC.Converter({ from: "cn", to: "tw" });

// 3. Convert - Done!
const result = await converter("简体中文");
console.log(result);  // 簡體中文
📦 Node.js (NPM)
// Install: npm install opencc-wasm
import OpenCC from "opencc-wasm";

const converter = OpenCC.Converter({ from: "cn", to: "tw" });
const result = await converter("简体中文");
console.log(result);  // 簡體中文

📋 API Reference

OpenCC.Converter() - 建立轉換器

兩種指定轉換方式:

方式一:使用 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("服务器");  // 伺服器

支援的配置 Supported Configurations

Config 說明 Description
s2twp 簡體 → 台灣正體(含地域用詞轉換)
s2tw 簡體 → 台灣正體
s2hk 簡體 → 香港繁體
s2t 簡體 → OpenCC 標準繁體
tw2s 台灣正體 → 簡體
tw2sp 台灣正體 → 簡體(含地域用詞轉換)
hk2s 香港繁體 → 簡體
t2s OpenCC 標準繁體 → 簡體
hk2t / t2hk 香港繁體 ↔ OpenCC 標準繁體
tw2t / t2tw 台灣正體 ↔ OpenCC 標準繁體
jp2t / t2jp 日文新字體 ↔ 舊字體
t2cngov 繁體或簡體 → 通用規範漢字表繁體 (來自TerryTian-Tech) 試用
t2cngov_keep_simp 繁體或簡體 → 通用規範漢字表繁體(保留簡體字) (來自TerryTian-Tech)

OpenCC.CustomConverter() - 自訂轉換器

const converter = OpenCC.CustomConverter([
  ["“", "「"],
  ["”", "」"],
  ["‘", "『"],
  ["’", "』"],
]);

const result = converter("这是“引号”和‘单引号’");
// Output: 这是「引号」和『单引号』

💡 使用範例 Examples

⚛️ React
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>
  );
}
💚 Vue 3
<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>

🔧 最佳實踐 Best Practices

✅ 重複使用轉換器實例

// ✅ 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("繁體"));   // 繁体

線上示範 Demo Pages