BergamotTranslatorSharp 0.4.1

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

BergamotTranslatorSharp

App Build GitHub Release NuGet Version NuGet Downloads

BergamotTranslatorSharp is a C# wrapper for Bergamot Translator. It allows .NET applications to use an offline machine translation engine.

| EN | JA |

Overview

Bergamot Translator is an offline translation engine. The official website is https://browser.mt/. This library wraps its functionality for use from C#.

Features

  • Offline translation capability
  • Multi-language support
  • Fast processing
  • HTML markup preservation
  • Easy integration with .NET applications

Installation

Install from NuGet

Install-Package BergamotTranslatorSharp

Or:

dotnet add package BergamotTranslatorSharp

Requirements

  • .NET 8.0 or later
  • Windows x64, Windows ARM64, Linux x64, or macOS ARM64

Build the native library from source

Windows ARM64 builds use vcpkg and Visual Studio's ARM64 clang-cl toolchain. From an ARM64 developer prompt, set VCPKG_ROOT and run:

set VCPKG_ROOT=C:\vcpkg
cmake --preset windows-arm64-clangcl-release
cmake --build --preset windows-arm64-clangcl-release
cmake --install out\build\windows-arm64-clangcl-release --prefix libs --component bergamot_translator_dynamic

Usage

1. Download models

Download and extract models from Firefox Translations models.

git clone --depth 1 --branch main --single-branch https://github.com/mozilla/firefox-translations-models/
mkdir models
cp -rf firefox-translations-models/registry.json models
cp -rf firefox-translations-models/models/prod/* models
cp -rf firefox-translations-models/models/dev/* models
gunzip models/*/*

After extraction, choose the model directory for the translation direction you want to use. For example, use a deen model directory for German to English, or an enja model directory for English to Japanese.

2. Create a configuration file

Create a config.yml or config.txt file in the same directory as the model files.

Example for an English to Japanese model:

relative-paths: true
models:
- model.enja.intgemm.alphas.bin
vocabs:
- vocab.enja.spm
- vocab.enja.spm
shortlist:
- lex.50.50.enja.s2t.bin
- false
beam-size: 1
normalize: 1.0
word-penalty: 0
max-length-break: 128
mini-batch-words: 1024
workspace: 128
max-length-factor: 2.0
skip-cost: true
cpu-threads: 0
quiet: true
quiet-translation: true
gemm-precision: int8shiftAlphaAll

Notes:

  • The file names in models, vocabs, and shortlist must match the files in the selected model directory.
  • If the model file name contains alphas, use gemm-precision: int8shiftAlphaAll.
  • Otherwise, use gemm-precision: int8shiftAll.
  • When relative-paths: true is used, keep the configuration file and the model files together, or update the paths accordingly.

3. Translate text from C#

The current API uses BlockingService. Pass one or two configuration file paths to the constructor.

using BergamotTranslatorSharp;

var configPath = Path.Combine(
    AppDomain.CurrentDomain.BaseDirectory,
    "models",
    "enja",
    "config.txt");

using var service = new BlockingService(configPath);

var translated = service.Translate("Hello, world!");

Console.WriteLine(translated);

To translate text content while preserving HTML markup, pass true as the second argument:

var translatedHtml = service.Translate("<p>Hello, <strong>world</strong>!</p>", html: true);

If you pass one configuration file path, BlockingService uses that model directly. If you pass two configuration file paths, the native service uses them as a pivot translation chain.

4. Run the managed sample

ManagedSample follows this argument format:

ManagedSample [--html] <config-paths>[..] <text>

All arguments except the last one are treated as configuration file paths. The last argument is treated as the source text.

Example:

dotnet run --project ManagedSample -- ./models/enja/config.txt "Hello, world!"

For HTML input, add --html before the configuration paths:

dotnet run --project ManagedSample -- --html ./models/enja/config.txt "<p>Hello, <strong>world</strong>!</p>"

For pivot translation, pass two configuration files before the text:

dotnet run --project ManagedSample -- ./models/source-pivot/config.txt ./models/pivot-target/config.txt "Hello, world!"

Troubleshooting

Failed to create translator instance

This usually means the model could not be loaded. Check the following:

  • The configuration file path is correct.
  • The model, vocabulary, and shortlist files exist.
  • The file names in the configuration file match the actual files.
  • gemm-precision matches the model type.
  • The native bergamot library can be loaded on your platform.

No translation or unexpected output

Check that the model direction matches the input language. For example, a deen model is intended for German to English input, while an enja model is intended for English to Japanese input.

License

This project is released under the MPL-2.0 license.

Contribution

Please report bugs and feature requests to the GitHub Issue Tracker. Pull requests are also welcome.

Acknowledgments

This project is based on browsermt/bergamot-translator.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on BergamotTranslatorSharp:

Repository Stars
Freeesia/WindowTranslator
WindowTranslatorは、Windowsのアプリケーションのウィンドウを翻訳するためのツールです。
Version Downloads Last Updated
0.4.1 105 7/7/2026
0.3.4 1,417 10/5/2025
0.3.3 938 3/29/2025
0.3.1 219 3/29/2025
0.3.0 570 3/26/2025
0.2.1 193 3/22/2025
0.1.1 187 3/22/2025
0.1.0 190 3/21/2025
0.0.1 247 3/20/2025