OpenccFmmsegLib 1.3.1
dotnet add package OpenccFmmsegLib --version 1.3.1
NuGet\Install-Package OpenccFmmsegLib -Version 1.3.1
<PackageReference Include="OpenccFmmsegLib" Version="1.3.1" />
<PackageVersion Include="OpenccFmmsegLib" Version="1.3.1" />
<PackageReference Include="OpenccFmmsegLib" />
paket add OpenccFmmsegLib --version 1.3.1
#r "nuget: OpenccFmmsegLib, 1.3.1"
#:package OpenccFmmsegLib@1.3.1
#addin nuget:?package=OpenccFmmsegLib&version=1.3.1
#tool nuget:?package=OpenccFmmsegLib&version=1.3.1
OpenccFmmsegLib
A .NET Standard 2.0 library providing a managed C# wrapper for the Rust-based opencc-fmmseg C API (OpenCC-compatible), enabling high-performance Chinese text conversion (Simplified / Traditional) in .NET applications.
This library focuses only on OpenCC-style conversion. For Jieba segmentation and keyword extraction, use OpenccJiebaLib instead.
Features
- OpenCC-compatible Chinese text conversion (Simplified / Traditional variants)
- Optional punctuation conversion
- Fast FMM-based segmentation engine under the hood (native Rust)
- Lightweight API with explicit native error-reporting semantics
- Safe native resource management via
IDisposable
Supported Conversion Configurations
s2t, t2s, s2tw, tw2s, s2twp, tw2sp, s2hk, hk2s,
t2tw, t2twp, t2hk, tw2t, tw2tp, hk2t, t2jp, jp2t
Getting Started
Prerequisites
- .NET Standard 2.0 or higher (.NET Framework, .NET Core / 5+ / 6+, Mono, Xamarin, etc.)
- .NET 6.0 or later recommended
- Native
opencc_fmmseg_capilibrary available at runtime
Installation
Option 1 — From NuGet (recommended)
dotnet add package OpenccFmmsegLib
The NuGet package includes prebuilt native runtimes and deploys them under:
runtimes/<RID>/native/
Shipped RIDs:
win-x64linux-arm64linux-x64osx-x64osx-arm64
No manual copying is required when using NuGet.
Option 2 — Project Reference / Custom Native Builds
If you use a project reference or a custom native build, place the native library using the same layout as NuGet:
runtimes/<RID>/native/
Expected filenames:
- Windows:
opencc_fmmseg_capi.dll - Linux:
libopencc_fmmseg_capi.so - macOS:
libopencc_fmmseg_capi.dylib
The built-in native loader will discover the library automatically.
🧪 Unit test projects must also have access to the native library. Use the same layout under the test project output directory.
Usage
using OpenccFmmsegLib;
using var opencc = new OpenccFmmseg();
string input = "汉字转换测试";
string result = opencc.Convert(input, "s2t");
Console.WriteLine(result); // 漢字轉換測試
int code = opencc.ZhoCheck(input);
Console.WriteLine(code); // 2 (Simplified Chinese)
Error Handling
InvalidOperationExceptionis thrown if initialization fails or a native error occursOpenccFmmseg.LastError()returns the last native error message
Public API Overview
OpenccFmmseg
string Convert(string input, string config, bool punctuation = false)Converts Chinese text using the specified canonical OpenCC configuration name and throws for invalid names.string Convert(string input, OpenccConfig configId, bool punctuation = false)Converts Chinese text using the OpenCC configuration Enum.string ConvertCfg(string input, OpenccConfig configId, bool punctuation = false)Converts Chinese text using the numeric-config native path with a typed config enum.string ConvertCfgMemLen(string input, int configId, bool punctuation = false)Converts Chinese text using the optimized explicit-length native buffer pipeline.byte[] ConvertCfgMemLenToUtf8Z(string input, int configId, bool punctuation = false)Returns UTF-8 output including a trailing NUL terminator for interop scenarios.bool TryConvertCfgToUtf8(string input, int configId, bool punctuation, Span<byte> destination, out int requiredBytes)Writes UTF-8 output into a caller-provided buffer using the native size-query API.bool TryConvertCfgToUtf8Into(string input, int configId, bool punctuation, Span<byte> destination, out int requiredBytes)Writes UTF-8 output into a caller-provided buffer using the explicit-length native API.int ZhoCheck(string input)Detects whether the input text is Simplified Chinese, Traditional Chinese, or non-Chinese.static string LastError()Returns the last error message reported by the native library's shared error slot.Implements
IDisposablefor deterministic native resource cleanup.
Troubleshooting
DllNotFoundException / Unable to load shared library 'opencc_fmmseg_capi'
Ensure the native file exists under:
runtimes/<RID>/native/Clean and rebuild the project after installing via NuGet
Verify the correct file name for your platform
BadImageFormatException
- Architecture mismatch (x64 vs x86)
- Ensure your application and native library target the same architecture
Linux: library exists but cannot be loaded
Place the
.sonext to the executable or set:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)
macOS: Gatekeeper / quarantine issues
xattr -dr com.apple.quarantine libopencc_fmmseg_capi.dylib
Thread Safety
- Do not share a single
OpenccFmmseginstance across threads - Create one instance per thread or scope
- Dispose instances promptly (
usingis recommended)
License
MIT License. See LICENSE.
Acknowledgements
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v1.3.1
- Updated native dependency:
- opencc-fmmseg-capi upgraded to v0.9.2
- Tightened the public string-config conversion API: `Convert(string, string, ...)` now validates config names strictly
and throws for invalid values instead of silently falling back to the default config
- Refined public XML documentation for release: clarified exception behavior, documented shared native `LastError()`
semantics, and cleaned up IntelliSense output for advanced conversion APIs
- Refreshed README release-facing docs to match the actual public API and threading/error-reporting behavior