[C#]使用Microsoft Translator Soap API實作翻譯功能
if (String.IsNullOrEmpty(text))
return DEFAULT_DETECTED_LANG;
const string DETECT_API_URI_PATTERN = "http://api.microsofttranslator.com/V2/Http.svc/Detect?appId={0}&text={1}";
const string MATCH_PATTERN = "<[^<>]*>([^<>]*)<[^<>]*>";
WebRequest req = WebRequest.Create(String.Format(DETECT_API_URI_PATTERN, APP_ID, text));
WebResponse resp = req.GetResponse();
string local = DEFAULT_DETECTED_LANG;
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
local = reader.ReadToEnd();
local = Regex.Match(local, MATCH_PATTERN).Groups[1].Value;
}
return local;
}</pre>
if (!langs.Contains(lang))
lang = "en";
return client.Speak(APP_ID, tbxSource.Text, lang, "audio/wav", string.Empty);
}</pre>
var langNames = (from lang in langs.AsParallel()
select new { Name = Client.GetLanguageNames(APP_ID, lang, new string[] { lang })[0], Value = lang }).ToList();
cbTranslateTo.DisplayMember = "Name";
cbTranslateTo.ValueMember = "Value";
cbTranslateTo.DataSource = langNames;
if (cbTranslateTo.Items.Count > 0)
cbTranslateTo.Text = Client.GetLanguageNames(APP_ID, local, new string[] { local }).FirstOrDefault();
}</pre>
namespace TranslateAPIDemo { public partial class Form1 : Form { #region Const private const string APP_ID = “”; #endregion
#region Var
private LanguageServiceClient _client;
#endregion
#region Property
public LanguageServiceClient Client
{
get
{
if (_client == null)
_client = new LanguageServiceClient();
return _client;
}
}
#endregion
public Form1()
{
InitializeComponent();
}
string GetDetectedLanguage(string text)
{
const string DEFAULT_DETECTED_LANG = "en";
if (String.IsNullOrEmpty(text))
return DEFAULT_DETECTED_LANG;
const string DETECT_API_URI_PATTERN = "http://api.microsofttranslator.com/V2/Http.svc/Detect?appId={0}&text={1}";
const string MATCH_PATTERN = "<[^<>]*>([^<>]*)<[^<>]*>";
WebRequest req = WebRequest.Create(String.Format(DETECT_API_URI_PATTERN, APP_ID, text));
WebResponse resp = req.GetResponse();
string local = DEFAULT_DETECTED_LANG;
using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
{
local = reader.ReadToEnd();
local = Regex.Match(local, MATCH_PATTERN).Groups[1].Value;
}
return local;
}
string GetSpeakUri(LanguageServiceClient client, string text)
{
string[] langs = client.GetLanguagesForSpeak(APP_ID);
string lang = (cbTranslateTo.SelectedValue == null)? "en": cbTranslateTo.SelectedValue.ToString();
if (!langs.Contains(lang))
lang = "en";
return client.Speak(APP_ID, tbxSource.Text, lang, "audio/wav", string.Empty);
}
void UpdateTranslationLanguages()
{
string local = GetDetectedLanguage(tbxSource.Text);
string[] langs = Client.GetLanguagesForTranslate(APP_ID);
var langNames = (from lang in langs.AsParallel()
select new { Name = Client.GetLanguageNames(APP_ID, lang, new string[] { lang })[0], Value = lang }).ToList();
cbTranslateTo.DisplayMember = "Name";
cbTranslateTo.ValueMember = "Value";
cbTranslateTo.DataSource = langNames;
if (cbTranslateTo.Items.Count > 0)
cbTranslateTo.Text = Client.GetLanguageNames(APP_ID, local, new string[] { local }).FirstOrDefault();
}
private void button1_Click(object sender, EventArgs e)
{
string local = GetDetectedLanguage(tbxSource.Text);
string speakUri = GetSpeakUri(Client, tbxSource.Text);
if (speakUri.Length > 0)
{
axWindowsMediaPlayer1.URL = speakUri;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
if (cbTranslateTo.SelectedValue != null)
tbxTarget.Text = Client.Translate(APP_ID, tbxSource.Text, local, cbTranslateTo.SelectedValue.ToString(), "text/html", "general");
}
private void Form1_Load(object sender, EventArgs e)
{
UpdateTranslationLanguages();
}
}
}