offline-translator 1.3.0

dotnet add package offline-translator --version 1.3.0
                    
NuGet\Install-Package offline-translator -Version 1.3.0
                    
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="offline-translator" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="offline-translator" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="offline-translator" />
                    
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 offline-translator --version 1.3.0
                    
#r "nuget: offline-translator, 1.3.0"
                    
#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 offline-translator@1.3.0
                    
#: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=offline-translator&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=offline-translator&version=1.3.0
                    
Install as a Cake Tool

OfflineTranslator

适用于 .NET 6 的离线翻译类库,支持简体中文转繁体中文,通过 M2M100 或 Qwen 将中文翻译为英文、越南语,以及通过 Qwen 将英语翻译为越南语。越南语结果保留完整的 Unicode 声调符号,例如 Xin chào Việt Nam

运行时不访问网络,也不依赖 Python。模型下载完成后,整个 models 目录可以复制到离线电脑。

环境

  • .NET 6 或使用 --self-contained 发布
  • M2M100 支持 Windows x64、Linux x64 或 Linux arm64
  • Qwen 首期支持 Windows x64 CPU
  • CPU 版本不需要 CUDA
  • Windows 需要 Microsoft Visual C++ 2015-2022 x64 Redistributable

准备 M2M100 模型

推荐直接下载已经量化的 ONNX 模型,不需要 Python:

.\tools\download-onnx-models.ps1

脚本固定仓库提交并校验大文件的 SHA-256。当前网络默认使用 https://hf-mirror.com,可通过 -Endpoint 修改。

模型来源为 Xenova/m2m100_418M,基于 MIT 许可证的 facebook/m2m100_418M。中英和中越共用一套 INT8 量化模型,ONNX 文件合计约 606 MB。

模型权重不包含在 OfflineTranslator NuGet 包中。分发应用和模型时,需要同时遵守模型仓库中声明的第三方许可证及署名要求。

运行目录约定如下:

models/
  m2m100/
    encoder_model.onnx
    decoder_model.onnx
    tokenizer.json
    config.json

准备 Qwen 模型

Qwen 使用 ONNX Runtime GenAI 格式,不需要安装 Ollama。默认脚本下载 Qwen2.5 7B Instruct 1M INT4 模型,ONNX 外部权重约 7.1 GB,基础模型使用 Apache-2.0 许可证:

.\tools\prepare-qwen-model.ps1

脚本固定模型 revision,并校验 ONNX 图、外部权重和 tokenizer 大文件的 SHA-256。默认输出目录为 models\qwen

models/
  qwen/
    genai_config.json
    model.onnx
    model.onnx.data
    tokenizer.json
    tokenizer_config.json
    chat_template.jinja
    LICENSE
    MODEL_SOURCE.txt

Qwen2.5 3B 也能使用相同 API,但官方 3B 权重采用 Qwen Research License,仅允许非商业用途;商业项目必须另行取得许可。因此下载脚本默认使用 Apache-2.0 的 7B 模型。

使用

简体中文转台湾繁体中文不需要加载翻译模型:

using OfflineTranslator;

string traditional = ChineseTextConverter.ToTraditionalChinese("电镀减铜");

中文翻译为英文或越南语时创建并长期复用翻译器:

using OfflineTranslator;

using var translator = new ChineseOfflineTranslator(@"D:\App\models");

string english = translator.TranslateToEnglish("欢迎使用离线翻译。");
string vietnamese = translator.TranslateToVietnamese("欢迎使用离线翻译。");

也可以统一调用:

string result = translator.Translate(
    "今天天气很好。",
    TranslationLanguage.Vietnamese);

ChineseOfflineTranslator 会延迟加载 M2M100 模型。实例应长期复用,避免每次翻译都重新加载约 606 MB 的 ONNX 文件。

使用 Qwen ONNX Runtime GenAI 模型:

using OfflineTranslator;

using var translator = new QwenOfflineTranslator(@"D:\App\models\qwen");

string english = translator.TranslateToEnglish("设备运行正常。");
string vietnamese = translator.TranslateToVietnamese("设备运行正常。");
string englishToVietnamese = translator.TranslateEnglishToVietnamese(
    "The equipment is operating normally.");

Qwen 模型在当前 .NET 进程内运行,不启动本地服务,也不发送 HTTP 请求。模型首次加载会占用数 GB 内存,QwenOfflineTranslator 实例必须长期复用。当前版本对同一实例的翻译请求串行执行,以控制纯 CPU 环境的内存和线程竞争。

测试程序

项目只保留一个控制台测试程序:

dotnet run --project .\OfflineTranslator.Test\OfflineTranslator.Test.csproj

不传参数时会使用仓库中的 models 目录和默认中文句子,并打印繁体中文、英文和越南语结果。仅验证简繁转换时不需要模型:

dotnet run --project .\OfflineTranslator.Test\OfflineTranslator.Test.csproj -- --traditional "电镀减铜"

验证 Qwen 模型:

dotnet run --project .\OfflineTranslator.Test\OfflineTranslator.Test.csproj -- `
  --qwen .\models\qwen "欢迎使用离线翻译。"

验证英语翻译为越南语:

dotnet run --project .\OfflineTranslator.Test\OfflineTranslator.Test.csproj -- `
  --qwen-en-vi .\models\qwen "Welcome to offline translation."

运行同一测试项目中的无模型自检:

dotnet run --project .\OfflineTranslator.Test\OfflineTranslator.Test.csproj -- --self-test

NuGet 打包和引用

生成本地 NuGet 包:

dotnet pack .\OfflineTranslator\OfflineTranslator.csproj -c Release -o .\artifacts

业务项目添加本地包源后引用:

<PackageReference Include="offline-translator" Version="1.3.0" />

NuGet 包只包含类库、OpenCC 词典和依赖声明,不包含 M2M100 或 Qwen 权重。发布应用时将所需的模型目录单独复制,并把目录传给翻译器构造函数。

联网构建机还原 NuGet 依赖后,可以将完整发布目录和模型目录一起复制到离线机器。如果离线机器需要自行执行 dotnet restore,本地包源中还必须包含 ONNX Runtime、ONNX Runtime GenAI、OpenCCNET 及其传递依赖,不能只准备 offline-translator.1.3.0.nupkg

注意

  • 这里的“越南语带音标”指保留 ă â đ ê ô ơ ư 和声调符号。
  • 简体转繁体使用 OpenCC 词典,不加载 M2M100 模型。
  • M2M100 是通用翻译模型,固定产品名和行业术语建议在业务层增加术语表。
  • Qwen 使用固定翻译 Prompt,并采用确定性贪心生成;原文中的指令只作为待翻译数据处理。
  • Qwen2.5 3B 默认不适合商业分发,除非已经取得单独商业许可。
  • 如果需要额外输出 IPA 国际音标,需要加入单独的越南语 G2P/IPA 模型。
  • 模型首次加载会花费一些时间,后续调用会复用会话。
  • .NET 6 已结束微软官方支持;类库仍兼容 net6.0,新项目建议同时评估升级到当前 LTS。
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
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.0 75 7/21/2026
1.2.0 79 7/20/2026

新增 Qwen2.5 ONNX Runtime GenAI 引擎、中文到英语/越南语翻译及英语到越南语翻译。