Swift port of OpenCC, with bundled offline dictionaries.
- Swift 5.4 or later; C++17-compatible Apple toolchain.
- This fork is validated for the OpenCCman deployment targets: macOS 11 and iOS 14. It adds no newer OS API requirement.
- Initialize the OpenCC submodule when building a source checkout:
git submodule update --init --recursive.
import OpenCC
let converter = try ChineseConverter(options: [.traditionalize, .twStandard, .twIdiom])
converter.convert("鼠标里面的硅二极管坏了,导致光标分辨率降低。")
// 滑鼠裡面的矽二極體壞了,導致游標解析度降低。Every official OpenCC configuration is also available by name, including six
that no option combination reaches (s2hkp, hk2sp, tw2t, hk2t, t2jp,
jp2t):
let hongKong = try ChineseConverter(configuration: .s2hkp)
hongKong.convert("鼠标里面的硅二极管坏了,导致光标分辨率降低。")
// 滑鼠裏面的硅二極管壞了,導致游標分辨率降低。ChineseConverter.Configuration.allCases lists the 16 bundled configurations.
Each produces the same output as the OpenCC 1.4.2 command-line tool; see
official configurations.
ChineseConverter remains immutable and supports concurrent conversion. All five public option bits and their existing precedence are preserved, including the four mixed character/idiom combinations without an exact official configuration. Conversion accepts embedded U+0000 without truncating the suffix. Native handles are released when the Swift object is deinitialized.
Use an independent session for large files without retaining the complete input or output. Process returned bytes immediately, then write the final tail:
let stream = try converter.makeStream()
while let chunk = try input.read(upToCount: 256 * 1024), !chunk.isEmpty {
try output.write(contentsOf: stream.append(chunk))
}
try output.write(contentsOf: stream.finish())input and output are caller-owned FileHandle values. Serialize calls to each
mutable ChineseConversionStream; independent sessions can run concurrently
and may outlive their converter. The stream preserves embedded NUL, line endings
and BOM, matching complete-string conversion. File applications that omit the
leading BOM must strip it before appending. Chunk boundaries may split UTF-8
scalars or phrases. Malformed UTF-8 throws ConversionError.invalidUTF8;
finish() also rejects a truncated final scalar. Completion or any error makes
the session terminal (ConversionStreamError.closed on subsequent use).
Stream-only failures use ConversionStreamError, so exhaustive switches over
ConversionError keep compiling.
Discard partial output after an error; applications
6F93
should write to a temporary
file and commit it only after successful completion.
The bridge consumes complete dictionary/IDS match units, retaining bounded
lookahead from actual dictionary key lengths. It preserves normalization,
mmseg segment boundaries and conversion-chain order, including arbitrarily long
unmatched runs. All 14 option modes and all 16 official configurations are supported. Future unsupported
converter or segmenter implementations fail explicitly with
ConversionStreamError.unsupportedConfiguration. See
streaming validation for the algorithm and regression cases.
OpenCC 1.4.2 cannot interrupt a conversion: its C and C++ APIs have no cancel,
progress or deadline hook, and every native call runs to completion. Once
convert(_:) starts, a caller can only discard its result.
A stream can be abandoned between calls. Check for cancellation before each
append(_:), then release the session without calling finish(). This frees its
native state and leaves the converter and other sessions unaffected. The call
already running still completes, so cancellation takes effect within the time
needed to convert one chunk; smaller chunks respond sooner.
This fork follows ddddxxx/SwiftyOpenCC wrapper commits and stable BYVoid/OpenCC releases. The coordinator lives in OpenCCman/main: it proposes a fork PR, then an OpenCCman/build revision-update PR after this fork's merge commit passes CI. Each PR is reviewed and merged by a maintainer.
See the fork maintenance guide for source/resource updates, compatibility constraints and required checks. The shared coordinator guide covers scheduling, commands, credentials and rollback. Updating only the OpenCC submodule is insufficient: source, configurations, dictionaries and fixtures must stay in sync.
See the 1.4.2 migration record for test coverage, known semantic changes and the measured cold-start/memory tradeoff. Reproduce the engine comparison with:
python3 scripts/benchmark-engine.py --baseline /path/to/SwiftyOpenCC-at-53f200c --candidate . --output /tmp/engine-benchmark.jsonRun bash scripts/check-ios-resource-signing.sh to verify iOS resource packaging with Xcode Cloud's archive signing settings. See the resource bundle signing fix for the reproduced failure and validation boundary.
The Swift wrapper is available under the MIT license. OpenCC and its bundled dependencies retain their respective upstream licenses.