Archive Browser
Download AzureTranslationDemoV3.zip, last updated 11/04/2019 (45.48 KB)
Download- md5: 71243a5a58a5b2055e7ffbc8a12dbbbb
- sha1: b3ea3e69c98e9179121e56912434d6ffa37ed7e9
- sha256: 5a4c764a08816a7bfc3060ba0006883bf49134838680b88901018ef707a23650
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
// Migrating from Azure translation API version 2 to 3
// http://www.cyotek.com/blog/migrating-from-azure-translation-api-version-2-to-3
// Translating text with Azure cognitive services
// http://www.cyotek.com/blog/translating-text-with-azure-cognitive-services
// This work is licensed under the Creative Commons Attribution 4.0 International License.
// To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/.
// Found this example useful?
// https://www.paypal.me/cyotek
namespace Cyotek.Demo.AzureTranslation
{
public partial class MainForm : Form
{
#region Fields
private string[] _languages;
private TranslationClient _translationClient;
private TranslationClientV2 _translationClientV2;
#endregion
#region Constructors
public MainForm()
{
this.InitializeComponent();
}
#endregion
#region Methods
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!this.DesignMode)
{
this.Font = SystemFonts.DialogFont;
}
this.Text = Application.ProductName;
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
_translationClient = new TranslationClient(apiKeyTextBox.Text);
_translationClientV2 = new TranslationClientV2(apiKeyTextBox.Text);
localComboBox.Text = "de";
fromComboBox.Text = "en";
toComboBox.Text = "de";
_languages = new[] { "en", "fr", "de" };
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
using (AboutDialog dialog = new AboutDialog())
{
dialog.ShowDialog();
}
}
private void apiKeyTextBox_TextChanged(object sender, EventArgs e)
{
_translationClient.AuthorizationKey = apiKeyTextBox.Text;
_translationClientV2.AuthorizationKey = apiKeyTextBox.Text;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void FillItems(ListBox control, string[] items)
{
ListBox.ObjectCollection listItems;
control.BeginUpdate();
listItems = control.Items;
listItems.Clear();
foreach (string item in items.OrderBy(item => item))
{
listItems.Add(item);
}
control.EndUpdate();
}
private void FillItems(TextBox control, string[] items)
{
control.Text = string.Join(Environment.NewLine, items.OrderBy(item => item));
}
private void FillItems(ComboBox control, string[] items)
{
ComboBox.ObjectCollection listItems;
control.BeginUpdate();
listItems = control.Items;
listItems.Clear();
foreach (string item in items.OrderBy(item => item))
{
listItems.Add(item);
}
control.EndUpdate();
}
private void getLanguageNamesButton_Click(object sender, EventArgs e)
{
this.WrapRequest(() =>
{
string[] names;
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
if (v3RadioButton.Checked)
{
names = _translationClient.GetLocalizedLanguageNames(localComboBox.Text, _languages);
}
else
{
names = _translationClientV2.GetLocalizedLanguageNames(localComboBox.Text, _languages);
}
this.FillItems(namesListBox, names);
});
}
private void getLanguagesButton_Click(object sender, EventArgs e)
{
this.WrapRequest(() =>
{
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
if (v3RadioButton.Checked)
{
_languages = _translationClient.GetLanguages();
}
else
{
_languages = _translationClientV2.GetLanguages();
}
this.FillItems(languageListBox, _languages);
this.FillItems(localComboBox, _languages);
this.FillItems(fromComboBox, _languages);
this.FillItems(toComboBox, _languages);
languageCountLabel.Text = _languages.Length.ToString();
});
}
private void swapButton_Click(object sender, EventArgs e)
{
string temp;
temp = fromComboBox.Text;
fromComboBox.Text = toComboBox.Text;
toComboBox.Text = temp;
temp = sourceTextBox.Text;
sourceTextBox.Text = translatedTextBox.Text;
translatedTextBox.Text = temp;
}
private void translateButton_Click(object sender, EventArgs e)
{
this.WrapRequest(() =>
{
// ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
if (v3RadioButton.Checked)
{
translatedTextBox.Text = _translationClient.Translate(sourceTextBox.Text, fromComboBox.Text, toComboBox.Text);
}
else
{
translatedTextBox.Text = _translationClientV2.Translate(sourceTextBox.Text, fromComboBox.Text, toComboBox.Text);
}
});
}
private void v3RadioButton_CheckedChanged(object sender, EventArgs e)
{
_languages = null;
languageListBox.Text = string.Empty;
namesListBox.Text = string.Empty;
translatedTextBox.Text = string.Empty;
}
private void WrapRequest(Action action)
{
this.UseWaitCursor = true;
Cursor.Current = Cursors.WaitCursor;
try
{
action();
}
catch (Exception ex)
{
MessageBox.Show(ex.GetBaseException().Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Cursor.Current = Cursors.Default;
this.UseWaitCursor = false;
}
#endregion
}
}
Donate
This software may be used free of charge, but as with all free software there are costs involved to develop and maintain.
If this site or its services have saved you time, please consider a donation to help with running costs and timely updates.
Donate