Ofcas.Framework.Translation.AspNetCore 3.0.17

Ofcas.AspNetCore.Translation

ASP.NET Core wrapper for the Orgadata translation system TX. The wrapper will obey CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture, which is generally set by the ASP.NET Core Localization Middleware.

Quickstart

Setting the component up is done in 4 easy steps:

1. Add the services

Add the services of the translation component inside your Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    CultureInfo cultureDe = new("de");
    CultureInfo cultureEnUs = new("en-US");

    CultureInfo[] supportedCultures = new[]
    {
        cultureEnUs,
        cultureDe,
    }

    // Recommended/Optional: Sort the languages by their native name, so they appear in the right order when displayed.
    Array.Sort(
        supportedCultures,
        Comparer<CultureInfo>.Create(
            (x, y) => StringComparer.InvariantCultureIgnoreCase.Compare(x.NativeName, y.NativeName)
        )
    );

    // Recommended/Optional: Add ASP.NET Core Localization Middleware options, so CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture is set correctly.
    services.Configure<RequestLocalizationOptions>(options =>
    {
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
        options.DefaultRequestCulture = new RequestCulture(culture: cultureDe, uiCulture: cultureDe);
    });

    services.AddTxLocalization();
}

2. Add the request localization middleware (optional)

 public void Configure(IApplicationBuilder app) {
    app.UseRequestLocalization();
 }

3. Add translatable strings to controllers/services

The translator can be used in controllers and services by injecting the ITxLocalizer interface.

public class HomeController {

    private readonly ITxLocalizer _txLocalizer;

    public HomeController(ITxLocalizer txLocalizer) {
        _txLocalizer = txLocalizer ?? throw new ArgumentNullException(nameof(txLocalizer));;
    }

    public IActionResult Index(string name)
    {
        string message = txLocalizer.Tx("Hallo Welt. Hallo #1.", 0, 0, name);

        return Json(message);
    }
}

4. Let our employees translate the application

The translation system will automatically replace the indicies of the translations in the process.

public partial class HomeController {
    public IActionResult Index(string name)
    {
-       string message = txLocalizer.Tx("Hallo Welt. Hallo #1.", 0, 0, name);
+       string message = txLocalizer.Tx("Hallo Welt. Hallo #1.", 551, 1, name);

        return Json(message);
    }
}

The path to which the translations need to be exported is {YourApplicationRoot}/Translations/Resources/. Click to see an example inside the ORGADATA-ID. We recommend to create a README.md file in this folder to let your fellow engineers know that the folder contains generated files.

HTML escaping

The first parameter of the IViewLocalizer.Tx call is always unescaped.

⚠️ WARNING: This means you can only use it with trusted input. Never use a string for the first parameter that someone we do not trust can manipulate.

@Localizer.Tx("This string is <b>unescaped</b>.", 0, 0);

Formatting parameters of the IViewLocalizer.Tx call will obey normal escaping rules.

@Localizer.Tx("This string is <b>unescaped</b>. #1", 0, 0, "This is escaped.");

If you want to use HTML as a translation parameter, you need to explicitly pass an object that implements the interface IHtmlContent just like you would do in standard razor:

@* Escape everything *@

@Localizer.Tx("This part <span>#1</span>.", 0, 0, new HtmlString("<b>is bold</b>."));

@* or escape parts of the content *@

@code {
    HtmlContentBuilder contentBuilder = new HtmlContentBuilder();

    contentBuilder.Append("This tag <b>is escaped</b>. ");
    contentBuilder.AppendHtml("This tag <b>is not</b>.");
}

@Localizer.Tx("Look at this: #1, 0, 0, contentBuilder);

Localizing stuff a human translator can't understand

Sometimes we need to localize values that a human translator can't localize for us, because they can't understand the context of the value before translation. This is for example the case when we are trying to localize URLs to the orgadata.com website: A human translator does not know how to build a URL, that contains the path to the website in his language. In these cases rely on CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture:

string url;

if(string.Equals(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, "en", StringComparison.OrdinalIgnoreCase)) {
    url = "https://www.orgadata.com/en/about-us";
}
else {
    url = "https://www.orgadata.com/%C3%BCber-uns";
}

Showing the top 20 packages that depend on Ofcas.Framework.Translation.AspNetCore.

Version Downloads Last updated
3.0.17 10 10/30/2026
3.0.11 49 10/04/2025
3.0.10 29 10/04/2025
3.0.9 30 10/04/2025
3.0.8 30 10/04/2025