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
services.AddRazorPages().AddXLocalizer<LocSource,MyMemoryTranslateService>(ops =>{// ... ops.AutoTranslate =true; ops.TranslateFrom ="en";});
If TranslateFrom is not defined, then DefaultRequestCulture will be considered as the source culture for translation.
Use as standalone service
using XLocalizer.Translate;publicclassIndexModel:PageModel{privatereadonlyITranslator_translator;publicIndexModel(ITranslatortranslator){_translator=translator??thrownew NotImplementedException(nameof(translator));}publicvoidOnGet(){varsuccess= _translator.TryTranslate("en","tr","Welcome",outstring translation);}}
Or you can use the async method of ITranslator interface:
using XLocalizer.Translate;publicclassIndexModel:PageModel{privatereadonlyITranslator_translator;publicIndexModel(ITranslatorFactoryfactory){// 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>();