string speakurl = "";
// Creates a block within which an OperationContext object is in scope.
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
try
{
admToken = admAuth.GetAccessToken();
DateTime tokenReceived = DateTime.Now;
// Create a header with the access_token property of the returned token
headerValue = "Bearer " + admToken.access_token; //規定要加Bearer
return SpeakMethod(headerValue 翻譯公司 Text, SpeckLanguageCode);
}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
throw ex;
}
}
。-> 翻譯社|,-> 翻譯公司|的-> 翻譯 try
{
admToken = admAuth.GetAccessToken();
DateTime tokenReceived = DateTime.Now;
// Create a header with the access_token property of the returned token
headerValue = "Bearer " + admToken.access_token; //劃定要加Bearer
return TranslateMethod(headerValue, Text, FromLanguageCode 翻譯公司 ToLanguageCode);
}
catch (Exception ex)
{
throw ex;
}
}
catch (Exception ex)
{
throw ex;
}
}
private static AdmAuthentication admAuth = null;
對照了Google翻譯API與Microsoft翻譯API,因為微軟目前是免費所以採用微軟來殺青,
private static string SpeakMethod(string authToken, string sourceText, string SpeckLanguageCode)
{
MicrosoftTranslatorAPI.ServiceReference_MsTranslator.LanguageServiceClient client = new MicrosoftTranslatorAPI.ServiceReference_MsTranslator.LanguageServiceClient();
//Set Authorization header before sending the request
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Method = "POST";
httpRequestProperty.Headers.Add("Authorization", authToken);
if (admAuth == null)
admAuth = new AdmAuthentication(Utility._ClientID, Utility._ClientSecret);
//領受回應發音檔案 翻譯 Uri
private static string TranslateMethod(string authToken, string sourceText, string FromLanguageCode, string ToLanguageCode)
{
MicrosoftTranslatorAPI.ServiceReference_MsTranslator.LanguageServiceClient client = new MicrosoftTranslatorAPI.ServiceReference_MsTranslator.LanguageServiceClient();
//Set Authorization header before sending the request
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Method = "POST";
httpRequestProperty.Headers.Add("Authorization", authToken);
需要以下步調才能完成
public static string Translate(string Text 翻譯公司 string FromLanguageCode, string ToLanguageCode)
{
try
{
AdmAccessToken admToken;
string headerValue = "";
//Utility._ClientID=用戶識別碼此例為Riway,Utility._ClientSecret=用戶端密碼
//獲得回傳結果的Uri
要做一個用C#來告竣運用程式翻譯與播放發音,今朝想到最快 翻譯體例為利用已存在的API,
使用翻譯
2.發音
畫面參考
1.先到微軟市集微軟藍天阛阓 ( Microsoft Azure Marketplace ) 首頁 申請一個帳號 翻譯社
2.登入後找到 Microsoft Translator ,今朝是在資料頁籤 > 免費 > Microsoft Translator,或用搜索的。
3.進入後選擇註冊免費的每月限制200萬字元 翻譯項目如圖
4.獲得已註冊資料的用戶辨認碼與暗碼
4.1.到我的帳戶 > 開辟人員
4.2.按編纂獲得顯示的用戶端暗碼
string speakurl = "";
// Creates a block within which an OperationContext object is in scope.
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
// Keep appId parameter blank as we are sending access token in authorization header.
speakurl = client.Translate("" 翻譯公司 sourceText, FromLanguageCode, ToLanguageCode 翻譯公司 "text/plain", "general"); //language : en、zh-CHT contentType : "text/plain" and "text/html"
}
textBox_To.Text = TranslateAndSpeck.Translate(textBox_From.Text 翻譯公司 "en", "zh-CHT"); //參考MSDN有代號表此例為設定為英文轉中文
//念出英文傳入文字 與要念的語言代號 en、zh-CHT
return speakurl;
}
return speakurl;
}
//翻譯文字
參考 [C#]利用Microsoft Translator Soap API實作翻譯功能 一文
if (admAuth == null)
admAuth = new AdmAuthentication(Utility._ClientID 翻譯公司 Utility._ClientSecret);
步調1、申請 Microsoft Translator 來用:
1.翻譯
步調二、寫程式:
// Keep appId parameter blank as we are sending access token in authorization header.
speakurl = client.Speak("" 翻譯公司 sourceText, SpeckLanguageCode, "audio/wav", "MinSize"); //language : en、zh-CHT
}
public static string Speak(string Text, string SpeckLanguageCode)
{
try
{
AdmAccessToken admToken;
string headerValue = "";
以下文章來自: http://blog.xuite.net/riway62/blog/301024268-C%23+%E4%BD%BF%E7%94%A8%E5%BE%AE%E8%BB%9F%E7%BF%BB%E8%A有關翻譯的問題歡迎諮詢華碩翻譯社