# Plugin translations

Use BTCPay Server's localization APIs for user-facing text. Your plugin can register English source strings and bundle translations for other languages. The same UI then follows the translation dictionary selected for the current request, without a separate plugin language setting.

# Localize your text

Inject IStringLocalizer into a Razor view, or declare it once in your plugin's _ViewImports.cshtml:

@inject Microsoft.Extensions.Localization.IStringLocalizer StringLocalizer

<button type="submit">@StringLocalizer["Buy ticket"]</button>
<p>@StringLocalizer["Tickets for {0}", Model.EventName]</p>

You can inject the same interface into controllers and services. Look up text when it is needed rather than caching translated text in a singleton, because different requests can use different languages.

Use stable English text as the key and keep complete messages together. Put variable values in placeholders such as {0} and {1} instead of concatenating or interpolating them into the key. Translators can then reorder the words without changing the placeholders.

Keys are shared across core and all plugins, not scoped to a view or assembly. Choose clear, unambiguous wording when your plugin needs a different meaning from an existing string.

For static text, you can also use BTCPay Server's tag helper if your plugin's view imports include @addTagHelper *, BTCPayServer:

<span text-translate="true">Buy ticket</span>

Either way, register the English source strings as described below.

# Register the English source strings

Register your strings in the plugin's Execute(IServiceCollection) method:

using BTCPayServer;
using Microsoft.Extensions.DependencyInjection;

// Inside your plugin class:
public override void Execute(IServiceCollection services)
{
    services.AddDefaultTranslations(
        "Buy ticket",
        "Tickets for {0}");

    services.AddPluginTranslations(GetType().Assembly);
}

AddDefaultTranslations makes these strings available in the translation editor and the English catalog used by translators. Localizing a string in a view does not register it automatically. Keep existing registration if your plugin already provides these English defaults.

The second call registers your bundled translations, described below. It does not register English defaults or load an en.json file.

# Bundle translations with your plugin

Create a Translations folder in your plugin project with one JSON file per language code:

Translations/
  fr.json
  fr-FR.json
  ja.json

Only include the languages you support. For example, Translations/fr.json could contain:

{
  "Buy ticket": "Acheter un billet",
  "Tickets for {0}": "Billets pour {0}"
}

Use the same keys as your localization calls and preserve placeholders. Files must be valid JSON objects with string values. Missing, empty, or whitespace-only translations are ignored, so you can ship partial translations. Keys starting with _ are reserved for metadata and are not loaded.

Embed the files in your plugin assembly through its .csproj:

<ItemGroup>
  <EmbeddedResource Include="Translations/*.json" />
</ItemGroup>

If an existing resource rule already embeds these files, do not add them a second time. Files copied alongside the DLL as content are not enough: AddPluginTranslations reads embedded resources.

Build and install the plugin normally. Translation changes are distributed with plugin updates and become available after the server restarts.

# Understand language selection and fallbacks

The selected dictionary's language code, not its display name, determines which plugin file is used. For fr-FR, the localizer first tries fr-FR.json and then fills missing entries from fr.json. Language-code matching is case-insensitive.

For each key, the first available translation wins:

  1. Local overrides for the selected dictionary.
  2. Translations already stored in that dictionary.
  3. Plugin translations for its language code, then its base language.
  4. English defaults.

This lets your plugin fill gaps without replacing the selected dictionary's wording or an administrator's corrections. English (en) uses the English defaults rather than bundled plugin translations.

Bundled files do not install a new selectable language. An administrator must select a dictionary with a matching language code; custom dictionaries can set that code when they are created or edited. See Backend translations for the operator workflow.

Plugin translations remain fallbacks rather than being copied into the dictionary when its originals are saved. Later plugin updates can improve them without requiring the administrator to reimport translations.

# Handle HTML safely

Prefer StringLocalizer for plain text, including text with placeholders: Razor encodes the result. Do not wrap translated plain text in Html.Raw.

Use ViewLocalizer only when the translated sentence intentionally contains HTML. Encode dynamic arguments yourself:

@inject Microsoft.AspNetCore.Mvc.Localization.ViewLocalizer ViewLocalizer

<p>@ViewLocalizer["Tickets for <strong>{0}</strong>", Html.Encode(Model.EventName)]</p>

Register that complete HTML source string as an English default too, and keep its markup and placeholders valid in translations. Do not pass user-supplied text as unencoded HTML. For JavaScript, pass localized values through a JSON serializer rather than inserting them into quoted script strings.

# Other translation sources

Embedded JSON is the simplest option. If your translations come from another source, implement IPluginTranslationProvider.GetTranslations(languageCode) in BTCPayServer.Plugins.Translations and register it with services.AddPluginTranslationProvider<MyProvider>(). Return null when a language is unsupported. Providers are singletons, so keep their dependencies singleton-safe and avoid repeated expensive work.

English defaults are a separate contract: BTCPayServer.Services.IDefaultTranslationProvider, registered through services.AddTranslationProvider<MyDefaults>(). Use this instead of AddDefaultTranslations when your English catalog is generated dynamically, not for returning translated values.