TranslationProviderBase
Namespace: Wisej.Ext.Translation
Assembly: Wisej.Ext.Translation (4.1.0.0)
Base class for the translation providers.
- C#
- VB.NET
public abstract class TranslationProviderBase
Public MustInherit Class TranslationProviderBase
Derive from this class to connect the Translation component to a different translation service, then assign an instance to Provider or set ProviderType to the assembly-qualified name of the class.
Example:
public class MyTranslationProvider : TranslationProviderBase
{
public override string ClientID { get; set; }
public override string ClientSecret { get; set; }
public override TranslationResult Translate(string text, string from, string to)
{
try
{
string translated = MyTranslationService.Translate(this.ClientSecret, text, from, to);
return new TranslationResult(text, translated, from, to, 0, null);
}
catch (Exception ex)
{
return new TranslationResult(text, null, from, to, ex.HResult, ex.Message);
}
}
public override void TranslateAsync(string text, string from, string to, Action<TranslationResult> resultCallback)
{
Application.StartTask(() => resultCallback(Translate(text, from, to)));
}
}
// Use the custom provider.
this.translation1.Provider = new MyTranslationProvider();
Properties
ClientID
String: The client-id for the provider.
ClientSecret
String: The secret client-key or api-key for the provider.
Methods
Translate(text, from, to)
Invokes the translation service provider and returns the result of the request in an instance of the TranslationResult class.
| Parameter | Type | Description |
|---|---|---|
| text | String | The text to translate. |
| from | String | The source language code ("en", "de", ...), or null or empty to let the provider auto-detect the source language. |
| to | String | The target language code ("en", "de", ...). |
Returns: TranslationResult. An instance of TranslationResult.
Implementations should not throw when the service fails. Instead, return a TranslationResult with a null TranslatedText and a non-zero ErrorCode.
Example:
TranslationProviderBase provider = new TranslationProviderYandex { ClientSecret = "<your-api-key>" };
TranslationResult result = provider.Translate("Thank you", "en", "es");
if (result.ErrorCode == 0)
label1.Text = result.TranslatedText;
TranslateAsync(text, from, to, resultCallback)
Invokes the translation service provider asynchronously and returns the result of the request in an instance of the TranslationResult class.
| Parameter | Type | Description |
|---|---|---|
| text | String | The text to translate. |
| from | String | The source language code ("en", "de", ...), or null or empty to let the provider auto-detect the source language. |
| to | String | The target language code ("en", "de", ...). |
| resultCallback | Action<TranslationResult> | Callback method that will receive the TranslationResult when ready. |
Implementations should return immediately and invoke resultCallback once the result is available, typically from a task started with StartTask.
Example:
TranslationProviderBase provider = new TranslationProviderYandex { ClientSecret = "<your-api-key>" };
provider.TranslateAsync("Thank you", "en", "es", result =>
{
label1.Text = result.TranslatedText ?? result.ErrorMessage;
Application.Update(this);
});
Inherited By
| Name | Description |
|---|---|
| TranslationProviderYandex | Yandex translation provider. |