ZhEnOnnx 1.0.0

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

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 Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
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.0.0 99 8/20/2026