Auto translation support pack for XLocalizer
Install from nuget:
PM > Install-Package XLocalizer.Translate
Additionally install one or more translation service from nuget as well:
PM > Install-Package XLocalizer.Translate.GoogleTranslate
PM > Install-Package XLocalizer.Translate.YadexTranslate
PM > Install-Package XLocalizer.Translate.MyMemoryTranslate
PM > Install-Package XLocalizer.Translate.SystranTranslate
PM > Install-Package XLocalizer.Translate.IBMWatsonTranslate
Some translation services requires API keys, for all details visit: DOCS.Ziyad.info
Goto your project and add the keys to user secrets:
Right click on the project name and select Manage User Secrets
{
"XLocalizer.Translate": {
"RapidApiKey": "xxx-rapid-api-key-xxx"
}
}
{
"XLocalizer.Translate": {
"YandexTranslateApiKey": "xxx-yandex-translate-api-key-xxx"
}
}
{
"XLocalizer.Translate": {
"IBMWatsonTranslateApiKey": "xxx-imb-watson-cloud-api-key-xxx",
"IBMWatsonTranslateServiceUrl": "https// ibm-service-instance-url",
"IBMWatsonTranslateServiceVersionDate": "ibm-service-version-date"
}
}
- Register one or more services in startup:
services.AddSingleton<ITranslator, IBMWatsonStringTranslator>();
services.AddHttpClient<ITranslator, GoogleTranslateStringTranslator>();
services.AddHttpClient<ITranslator, YandexTranslateStringTranslator>();
services.AddHttpClient<ITranslator, MyMemoryTranslateStringTranslator>();
services.AddHttpClient<ITranslator, SystranTranslateStringTranslator>();
- Inject one or all services:
public class IndexModel : PageModel
{
private readonly ITranslator _translator;
public IndexModel(ITranslator translator)
{
_translator = translator;
}
public async Task<IActionResult> OnGetAsync()
{
var transResult = await _translator.TranslateAsync("en", "tr", "Hello world!", text);
}
}
The translation result is an object of type TranslationResult
.
Sample result for "Welcome" translation:
{ text: "Hoşgeldiniz", statusCode: 200, target: "tr", source: "en"}
If you want to add more services just implement the interface ITranslator
and register it in startup.