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.
- C#
- VB.NET
public class Translation : Component
Public Class Translation
Inherits Component
Constructors
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;
Translation(container)
Initializes a new instance of the Translation class together with the specified container.
| Name | Type | Description |
|---|---|---|
| container | IContainer | A IContainer that represents the container for the component. |
Throws:
- ArgumentNullException container is null.
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
ClientID
String: The client-id for the provider.
ClientSecret
String: The secret client-key or api-key for the provider.
Provider
TranslationProviderBase: Returns or sets the translation provider implementation.
ProviderType
String: Returns or sets the type of the translation provider.
Methods
Translate(text, from, to)
Translates the text to the requested language.
| 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. 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:
- ArgumentNullException text or to is null or empty.
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}");
}
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. |
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:
- ArgumentNullException text , to or resultCallback is null or empty.
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);
});
}