meta info
  • Title: Use Online Translation Services For Localization
  • Keywords: localization,, translate, online, service
  • Description: Learn how to use online translation services for localization of Asp.Net Core web apps with XLocalizer.Translate.
  • Author: Ziya Mollamahmut
  • Date: 23-Oct-2020
  • Image:
  • Image-alt: XLocalizer Logo
  • Version: v1.0

Use Online Translation Services For Localization

By Ziya Mollamahmut

Provides translation support to web applications. Can be used with XLocalizer or as standalsone services.

Table of contents

Available translation services

Below services are already available as nugets and ready to install.

Click on each service for more details about how to install.

Any service that implements ITranslator interface can be used for automatic translation with XLocalizer or as standalone translation services as well.

Use with XLocalizer

// Register a translation service
services.AddHttpClient<ITranslator, MyMemoryTranslateService>();

// Configure XLocalizer to use the translation service 
// and enable online translation
        .AddXLocalizer<LocSource, MyMemoryTranslateService>(ops =>
            // ...
            ops.AutoTranslate = true;
            ops.TranslateFrom = "en";

Recommended read IHttpClientFactory

If TranslateFrom is not defined, then DefaultRequestCulture will be considered as the source culture for translation.

Use as standalone service

using XLocalizer.Translate;

public class IndexModel : PageModel
    private readonly ITranslator _translator;

    public IndexModel(ITranslator translator)
        _translator = translator ?? throw new NotImplementedException(nameof(translator));

    public void OnGet()
        var success = _translator.TryTranslate("en", "tr", "Welcome", out string translation);

Or you can use the async method of ITranslator interface:

public async Task OnGetAsync()
    var translation = await _translator.TranslateAsync("en", "tr", "Welcome", "text");

Register multiple translation services

It is possible to register multiple tranlsation services then retrive specific service for translation using ITranslatorFactory interface.

Register multiple translation services:

services.AddHttpClient<ITranslator, MyMemoryTranslateService>();
services.AddHttpClient<ITranslator, GoogleTranslateService>();
services.AddHttpClient<ITranslator, MicrosoftTranslateService>();
services.AddSingleton<ITranslator, IBMWatsonTranslateService>();
services.AddHttpClient<ITranslator, SystranTranslateService>();
services.AddHttpClient<ITranslator, YandexTranslateService>();

Retrive specific service:

using XLocalizer.Translate;

public class IndexModel : PageModel
    private readonly ITranslator _translator;

    public IndexModel(ITranslatorFactory factory)
        // e.g: retrive an instance of MyMemoryTranslateService
        _translator = factory.Create<MyMemoryTranslateService>();

        // or retrive an instance based on string service name
        _translator = factory.Create("MyMemory Translate");

ITranslatorFactory is already registered by XLocalizer. If you are not using XLocalizer you need to register it in startup as below:

using XLocalizer.Translate;
using XLocalizer.Translate.MyMemoryTranslate;

services.AddSingleton<ITranslatorFactory, TranslatorFactory<MyMemoryTranslateService>();