Skip to main content

Translation

Namespace: Wisej.Ext.Translation

Assembly: Wisej.Ext.Translation (4.1.0.0)

The Translation component interfaces with several third party translation providers to provide language translation services to the application.

public class Translation : Component

Constructors​

Instance member Translation()​

Initializes a new instance of the Translation class.

The new instance uses TranslationProviderYandex as its default Provider.

Example:

// Create the component in code and translate a string.
var translation = new Translation
{
ClientSecret = "<your-api-key>"
};

TranslationResult result = translation.Translate("Good morning", "en", "it");
label1.Text = result.TranslatedText;

Instance member Translation(container)​

Initializes a new instance of the Translation class together with the specified container.

NameTypeDescription
containerIContainerA IContainer that represents the container for the component.

Throws:

Example:

// Typically generated by the designer in InitializeComponent(),
// so the component is disposed together with its container.
this.components = new System.ComponentModel.Container();
this.translation1 = new Wisej.Ext.Translation.Translation(this.components);
this.translation1.ClientSecret = "<your-api-key>";

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.

Instance member Provider​

TranslationProviderBase: Returns or sets the translation provider implementation.

Instance member ProviderType​

String: Returns or sets the type of the translation provider.

Methods​

Instance member Translate(text, from, to)​

Translates the text to the requested language.

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. A TranslationResult containing the translated text or, if the request failed, the error code and message.

The call blocks until the provider responds. The ClientID and ClientSecret values are passed to the Provider before each request. Errors returned by the service are not thrown; check ErrorCode instead. Use TranslateAsync to avoid blocking the request thread.

Throws:

Example:

private void buttonTranslate_Click(object sender, EventArgs e)
{
// Auto-detect the source language by passing null for "from".
TranslationResult result = this.translation1.Translate(this.textBoxSource.Text, null, "de");

if (result.ErrorCode == 0)
this.textBoxTarget.Text = result.TranslatedText;
else
AlertBox.Show($"Translation failed ({result.ErrorCode}): {result.ErrorMessage}");
}

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.

The method returns immediately. The callback is invoked on a background task when the provider responds; when it changes controls, call Application.Update(this) to push the changes to the browser.

Throws:

Example:

private void buttonTranslate_Click(object sender, EventArgs e)
{
this.translation1.TranslateAsync(this.textBoxSource.Text, "en", "fr", result =>
{
this.textBoxTarget.Text = result.ErrorCode == 0
? result.TranslatedText
: "Error: " + result.ErrorMessage;

// Update the client, since the callback runs in a background task.
Application.Update(this);
});
}