僅限於以下幾種語言間的翻譯:
解釋:前面貼出的可以翻譯的幾種語言,是系統給出的,並不是博主創造的,上面的代碼就是從系統中獲取所有支持的語言翻譯功能。
翻譯功能代碼如下:
private string LanguageConverter(Guid serviceGuid, string sourceContent)
{
string transliterated = null;
if ((sourceContent != null) && (sourceContent.Length > 0))
{
try
{
MappingService mapService = new MappingService(serviceGuid);
using (MappingPropertyBag bag = mapService.RecognizeText(sourceContent, null))
{
transliterated = bag.GetResultRanges()[0].FormatData(new StringFormatter());
}
}
catch (LinguisticException exc)
{
MessageBox.Show(exc.Message);
}
}
return transliterated;
}
解釋:通過serviceGuid初始化不同的翻譯器,serviceGuid就是下拉列表中選擇的語言的guid。
調用翻譯功能的代碼:
try
{
guidService = ((DataItem)comboBox1.SelectedItem).guid;
string result = LanguageConverter(guidService.GetValueOrDefault(), txtSource.Text);
if ((result != null) && (result.Length > 0))
{
txtResult.Text = result;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
解釋:略。
下載:Demo Code