/// Demonstrates getting an access token and using the token to translate. private static async Task TranslateAsync() { var translatorService = new TranslatorService.LanguageServiceClient(); var authTokenSource = new AzureAuthToken(SubscriptionKey); var token = string.Empty; try { token = await authTokenSource.GetAccessTokenAsync(); } catch (HttpRequestException) { switch (authTokenSource.RequestStatusCode) { case HttpStatusCode.Unauthorized: Console.WriteLine("Request to token service is not authorized (401). Check that the Azure subscription key is valid."); break; case HttpStatusCode.Forbidden: Console.WriteLine("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded."); break; } throw; } Console.WriteLine("Translated to French: {0}", translatorService.Translate(token, "Hello World", "en", "fr", "text/plain", "general", string.Empty)); }
private void DetectMethod(string authToken) { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); //Set Authorization header before sending the request HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Method = "POST"; httpRequestProperty.Headers.Add("Authorization", authToken); using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty; //Below line will return the code of the detected language. string[] strDetectedLangCode = { client.Detect("", txtUser.Text) }; detectedLangCode = strDetectedLangCode[0]; //Fetch the name of the detected language using the code. string[] strDetectedLang = client.GetLanguageNames("", "en", strDetectedLangCode, true); lblDetectedText.Text = strDetectedLang[0]; //Fetching the list of supported languages and binding to the dropdown list. languagesForTranslate = client.GetLanguagesForTranslate(""); allLang = client.GetLanguageNames("", "en", languagesForTranslate, true); drpAllLang.DataSource = allLang; drpAllLang.DataBind(); } }
public static async Task <string> TranslateAsync(string text) { var translatorService = new TranslatorService.LanguageServiceClient(); var authTokenSource = new AzureAuthToken(SubscriptionKey); var token = string.Empty; try { token = await authTokenSource.GetAccessTokenAsync(); } catch (HttpRequestException) { switch (authTokenSource.RequestStatusCode) { case HttpStatusCode.Unauthorized: Console.WriteLine("Request to token service is not authorized (401). Check that the Azure subscription key is valid."); break; case HttpStatusCode.Forbidden: Console.WriteLine("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded."); break; } throw; } if (text.Contains("?vn")) { return(translatorService.Translate(token, text, "en", "vi", "text/plain", "general", string.Empty)); } else { return(translatorService.Translate(token, text, "vi", "en", "text/plain", "general", string.Empty)); } }
public static string translate_Wiki_Markup(string source) { string AR = ""; try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); int Max = source.Length / 8; if (Max > 8000) Max = 8000; int i; for (i = 0; i < source.Length; i += Max) { AR += client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", source.Substring(i, Max), "en", "ar"); } if (i - Max < source.Length - 1) { AR += client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", source.Substring(i - Max, source.Length - i - 1 + Max), "en", "ar"); } } catch (Exception ex) { // AR = AR.Replace("الطبقة = \"wikitable قابل للفرز\"", "class=\"wikitable sortable\""); return AR; MessageBox.Show(ex.Message); } return AR; }
public string Translate(string html, string langFrom, string langTo) { var translatorService = new TranslatorService.LanguageServiceClient(); var authTokenSource = new AzureAuthToken(subscriptionKey); try { if (token == null) { token = authTokenSource.GetAccessToken(); } } catch (HttpRequestException) { switch (authTokenSource.RequestStatusCode) { case HttpStatusCode.Unauthorized: Console.WriteLine("Request to token service is not authorized (401). Check that the Azure subscription key is valid."); break; case HttpStatusCode.Forbidden: Console.WriteLine("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded."); break; } throw; } return(translatorService.Translate(token, html, langFrom, langTo, "text/html", "general", string.Empty)); }
public async void Translate(int x, int y) { string rs = null; textBoxLog.Clear(); SendKeys.SendWait("^(c)"); async Task Puttaskdelay() { await Task.Delay(50); } await Puttaskdelay(); string input = Clipboard.GetText(); try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); rs = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", input, "", "vi"); Log(input); cm_trans.Items.Clear(); cm_trans.Items.Add(rs); cm_trans.Show(this, x, y); } catch (Exception ex) { Log(input); cm_trans.Items.Clear(); cm_trans.Items.Add(rs); cm_trans.Show(this, x, y); } }
private static String TranslateMethod(string authToken, string text, string lang) { // Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); //Set Authorization header before sending the request HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Method = "POST"; httpRequestProperty.Headers.Add("Authorization", authToken); // 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. return client.Translate("", text, "en", lang, "text/html", "general"); } }
public WordResp WordLookup(WordReq req) { var translator = new TranslatorService.LanguageServiceClient(); string txt = translator.Translate("C1E6D88CE2967328BBA9BC6C932B9D177247CAE5", req.Word, "en", "pt", null, null); if (!string.IsNullOrEmpty(txt)) { var dictionary = new DictService.DictServiceSoapClient("DictServiceSoap"); var definition = dictionary.Define(txt); if (definition != null && definition.Definitions.Count() > 0) { return new WordResp() { Exists = true, Description = definition.Definitions[0].WordDefinition }; } } return new WordResp() { Exists = false }; }
protected void BingTranslation() { String apiKey = Properties.Settings.Default.ApiKey; try { String fromLanguage = bingLanguagesAbr[cbFrom.SelectedIndex]; String toLanguage = bingLanguagesAbr[cbTo.SelectedIndex]; TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient("BasicHttpBinding_LanguageService"); String translatedLine = client.Translate(apiKey, tbOriginal.Text, fromLanguage, toLanguage); tbTranslated.Text = translatedLine; } catch (Exception e) { MessageBox.Show(e.Message + e.StackTrace, "Error"); throw new Exception(); } }
private void TranslateMethod(string auToken) { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); //Set Authorization header before sending the request HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Method = "POST"; httpRequestProperty.Headers.Add("Authorization", auToken); // Creates a block within which an OperationContext object is in scope. using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty; string translationResult; languagesForTranslate = client.GetLanguagesForTranslate(""); detectedLangCode = client.Detect("", txtUser.Text); translationResult = client.Translate("", txtUser.Text, detectedLangCode, languagesForTranslate[drpAllLang.SelectedIndex], "text/html", "general", ""); txtTranslated.Text = translationResult; } }
private static String TranslateMethod(string authToken, string toTranslate, string to, string from = "en") { // Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); //Set Authorization header before sending the request HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Method = "POST"; httpRequestProperty.Headers.Add("Authorization", authToken); string translationResult = null; // 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. translationResult = client.Translate("", toTranslate, from, to, "text/plain", "general"); } return(translationResult); }
// Translate an array of strings to a given language. // Pass an integer by reference (i.e. error_count) to track how many (if any) translations fail. // Returns a list of strings in the same order as the original array. public static List<string> TranslateArray(string[] texts, string lang, ref int error_count, string client_id = null, string client_secret = null) { string authToken = GenerateAuthToken(); TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); //Set Authorization header before sending the request HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Method = "POST"; httpRequestProperty.Headers.Add("Authorization", authToken); // Define options for all text strings TranslatorService.TranslateOptions options = new TranslatorService.TranslateOptions(); options.ContentType = "text/html"; // Track output, whether we get a valid translation or fallback to the original string on error. List<string> translations = new List<string>(); // 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. //return client.Translate("", text, "en", lang, "text/html", "general"); TranslatorService.TranslateArrayResponse[] results = client.TranslateArray( "", texts, "en", // from English lang, // to given language options // options for entire array of strings ); // Iterate translation output int i; for (i = 0; i < results.Length; i += 1) { // Convenience TranslatorService.TranslateArrayResponse result = results[i]; // Error check if (result.Error != null) { // Increment by-reference error counter error_count += 1; // Fall back to original string translations.Add(texts[i]); } else { // Add translated output translations.Add(result.TranslatedText); } } } // Return translations return translations; }
private void button1_Click(object sender, EventArgs e) { string strTranslatedText = null; string b; b = listBox1.Text; if (b == "Arabic") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "ar", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Chinese (Simplified)") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "zh-CHS", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "English") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "en", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "French") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "fr", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "German") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "de", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Greek") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "el", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Italian") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "it", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Japanese") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "ja", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Russian") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "ru", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Spanish") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "es", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Dutch") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "nl", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Indonesian") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "id", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Korean") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "ko", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Portuguese") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "pt", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (b == "Turkish") { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); client = new TranslatorService.LanguageServiceClient(); strTranslatedText = client.Translate("6CE9C85A41571C050C379F60DA173D286384E0F2", textbox1.Text, "", "tr", "text/plain", "general"); textbox2.Text = strTranslatedText; } catch { MessageBox.Show("Please Enter Text Correctly!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Aw Snap!" + "\nPlease Select Language Option Correctly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// συνάρτηση μετάφρασης /// </summary> /// <param name="sourceText"> κείμενο προς μετάφραση</param> /// <returns>μεταφρασμένο κείμενο</returns> public static string translate(string sourceText) { data = new RegUserPreferences(); string fromLanguage = data.FromLanguageShort; if (data.FromLanguageShort.Equals("auto")) { fromLanguage = detect(sourceText); if (fromLanguage.Equals("TranslatorException")) { init(); fromLanguage = detect(sourceText); } } try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Headers.Add("Authorization", headerValue); using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty; string translationResult; translationResult = client.Translate("", sourceText, fromLanguage, data.ToLanguageShort, "text/plain", ""); return translationResult; } } catch (Exception ) { //return (ex.Message); return "TranslatorException"; } }
/// <summary> /// συνάρτηση που ελέγχει σε τι γλώσσα είναι το κείμενο που δίνεται σαν παράμετρος, /// χρησιμοποιείται για την αυτόματη αναγνώριση /// </summary> /// <param name="sourceText">κείμενο που γίνεται ο έλεγχος της γλώσσας προέλευσης</param> /// <returns>κωδικός γλώσσας προέλευσης</returns> public static string detect(string sourceText) { try { TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient(); HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); httpRequestProperty.Headers.Add("Authorization", headerValue); using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty; string translationResult; translationResult = client.Detect("", sourceText); return translationResult; } } catch (Exception) { //return (ex.Message); return "TranslatorException"; } }