ZhEnOnnx 1.0.0
dotnet add package ZhEnOnnx --version 1.0.0
NuGet\Install-Package ZhEnOnnx -Version 1.0.0
<PackageReference Include="ZhEnOnnx" Version="1.0.0" />
<PackageVersion Include="ZhEnOnnx" Version="1.0.0" />
<PackageReference Include="ZhEnOnnx" />
paket add ZhEnOnnx --version 1.0.0
#r "nuget: ZhEnOnnx, 1.0.0"
#:package ZhEnOnnx@1.0.0
#addin nuget:?package=ZhEnOnnx&version=1.0.0
#tool nuget:?package=ZhEnOnnx&version=1.0.0
ZhEnOnnx
ZhEnOnnx is a Windows .NET translation library for the supplied Marian ONNX models.
It supports Chinese-to-English and English-to-Chinese translation, preferring DirectML GPU
inference and automatically falling back to CPU when DirectML cannot initialize.
Add a project reference
<ItemGroup>
<ProjectReference Include="..\ZhEnOnnx\ZhEnOnnx.csproj" />
</ItemGroup>
Use the translator
The models\zh-en and models\en-zh folders are part of this library. They are copied next
to your application automatically, so no model path is required.
using ZhEnOnnx;
using var translator = new BilingualTranslator();
string english = translator.TranslateToEnglish("你好,世界!");
string chinese = translator.TranslateToChinese("Machine learning is useful.");
Console.WriteLine(translator.Backend); // DirectMl or Cpu
To force CPU inference, use new BilingualTranslator(preferGpu: false). You can also provide
a custom model directory with new BilingualTranslator(modelRoot); it must contain the zh-en
and en-zh subdirectories.
Publish as a NuGet package
The package includes both models (about 856 MB total). Create a package with:
dotnet pack -c Release --no-restore
When another .NET project installs that package, its build output and published output receive a
models folder automatically. The parameterless constructor then works unchanged.
Improve English-to-Chinese output
English-to-Chinese calls split multi-sentence input, normalize Chinese punctuation and remove
unwanted whitespace by default. Use Terminology for deterministic replacements of model output.
var options = new TranslationOptions
{
Terminology = new Dictionary<string, string>
{
["机器 学习"] = "机器学习",
["ONNX运行时"] = "ONNX Runtime"
}
};
string chinese = translator.TranslateToChinese("Machine learning is useful.", options);
Translate C# identifiers
PascalCase, camelCase and underscore identifiers are expanded into normal words before
English-to-Chinese translation. For example, BilingualTranslator becomes Bilingual Translator
and _service becomes service, so they can be translated semantically.
var options = new TranslationOptions
{
ExpandCodeIdentifiers = true
};
string chinese = translator.TranslateToChinese(
"BilingualTranslator uses _service to configure service.",
options);
Set PreserveCodeIdentifiers = true when identifiers must remain unchanged in translated output.
Return a C# identifier from Chinese-to-English translation
Use EnglishOutputIdentifierStyle to remove spaces and punctuation from an English translation.
var options = new TranslationOptions
{
EnglishOutputIdentifierStyle = EnglishIdentifierStyle.CamelCase
};
string identifier = translator.TranslateToEnglish("双语翻译器", options);
// Example result: bilingualTranslator
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
-
net10.0-windows7.0
- Microsoft.ML.OnnxRuntime.DirectML (>= 1.24.4)
- Microsoft.ML.Tokenizers (>= 2.0.0)
- ZhEnOnnx.Models.EnZh.Decoder (>= 1.0.0)
- ZhEnOnnx.Models.EnZh.Encoder (>= 1.0.0)
- ZhEnOnnx.Models.ZhEn.Decoder (>= 1.0.0)
- ZhEnOnnx.Models.ZhEn.Encoder (>= 1.0.0)
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.0.0 | 99 | 8/20/2026 |