OpenccFmmsegLib 1.3.1

dotnet add package OpenccFmmsegLib --version 1.3.1
                    
NuGet\Install-Package OpenccFmmsegLib -Version 1.3.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenccFmmsegLib" Version="1.3.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenccFmmsegLib" Version="1.3.1" />
                    
Directory.Packages.props
<PackageReference Include="OpenccFmmsegLib" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenccFmmsegLib --version 1.3.1
                    
#r "nuget: OpenccFmmsegLib, 1.3.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenccFmmsegLib@1.3.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenccFmmsegLib&version=1.3.1
                    
Install as a Cake Addin
#tool nuget:?package=OpenccFmmsegLib&version=1.3.1
                    
Install as a Cake Tool

OpenccFmmsegLib

NuGet NuGet Downloads License

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_capi library available at runtime

Installation

dotnet add package OpenccFmmsegLib

The NuGet package includes prebuilt native runtimes and deploys them under:

runtimes/<RID>/native/

Shipped RIDs:

  • win-x64
  • linux-arm64
  • linux-x64
  • osx-x64
  • osx-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

  • InvalidOperationException is thrown if initialization fails or a native error occurs
  • OpenccFmmseg.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 IDisposable for 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 .so next 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 OpenccFmmseg instance across threads
  • Create one instance per thread or scope
  • Dispose instances promptly (using is recommended)

License

MIT License. See LICENSE.


Acknowledgements

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.1 86 4/21/2026
1.3.0 99 3/23/2026
1.2.1 233 10/28/2025
1.2.0 226 10/22/2025
1.1.0 205 10/7/2025
1.0.0 271 8/27/2025

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