Skip to main content

TranslationProviderBase

Namespace: Wisej.Ext.Translation

Assembly: Wisej.Ext.Translation (4.1.0.0)

Base class for the translation providers.

public abstract 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​

Instance member ClientID​

String: The client-id for the provider.

Instance member ClientSecret​

String: The secret client-key or api-key for the provider.

Methods​

Instance member Translate(text, from, to)​

Invokes the translation service provider and returns the result of the request in an instance of the TranslationResult class.

ParameterTypeDescription
textStringThe text to translate.
fromStringThe source language code ("en", "de", ...), or null or empty to let the provider auto-detect the source language.
toStringThe 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;

Instance member 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.

ParameterTypeDescription
textStringThe text to translate.
fromStringThe source language code ("en", "de", ...), or null or empty to let the provider auto-detect the source language.
toStringThe target language code ("en", "de", ...).
resultCallbackAction<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​

NameDescription
TranslationProviderYandexYandex translation provider.