private static void TranslateGroup(ResourceFile resourceFile, ResXResourceWriter writer, List <ResourceEntry> resourceGroup) { string[] texts = (from t in resourceGroup select t.OriginalValueWithPlaceHolders).ToArray(); TranslatorService.TranslateOptions options = new TranslatorService.TranslateOptions(); TranslatorService.TranslateArrayResponse[] translatedTexts = null; if (Utilities.FormUtility.HasValidMTSCredentials) { translatedTexts = TranslateUsingMTSService(resourceFile, texts, options); } else if (Utilities.FormUtility.HasValidAppId) { translatedTexts = TranslateUsingBingAppId(resourceFile, texts, options); } int j = 0; foreach (TranslatorService.TranslateArrayResponse translationResponse in translatedTexts) { resourceGroup[j].ReplacePlaceHoldersWithTokens(translationResponse.TranslatedText); writer.AddResource(resourceGroup[j].Key, resourceGroup[j].TranslatedValue); j++; } }
private static Dictionary <string, string> GetCulturesDataSource() { Dictionary <string, string> cultureDataSource = new Dictionary <string, string>(); string[] languageCodes = null; string[] languageNames = null; if (HasValidMTSCredentials && Utilities.FormUtility.UseMTS) { try { TranslatorService.TranslateOptions options = new TranslatorService.TranslateOptions(); using (OperationContextScope scope = new OperationContextScope(MTSLanguageServiceClient.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = MTSAccessHeader; //Keep appId parameter blank as we are sending access token in authorization header. languageCodes = MTSLanguageServiceClient.GetLanguagesForTranslate(""); languageNames = MTSLanguageServiceClient.GetLanguageNames(BingAppId, CultureInfo.CurrentUICulture.Name, languageCodes); } } catch (WebException wex) { LogUtility.WriteError(wex.ToString()); } catch (Exception ex) { LogUtility.WriteError(ex.ToString()); } } else if (HasValidAppId && Utilities.FormUtility.UseBing) { languageCodes = MTSLanguageServiceClient.GetLanguagesForTranslate(BingAppId); languageNames = MTSLanguageServiceClient.GetLanguageNames(BingAppId, CultureInfo.CurrentCulture.Name, languageCodes); } if (languageCodes != null && languageCodes.Length > 0 && languageNames != null && languageNames.Length > 0 && languageCodes.Length == languageNames.Length) { for (int i = 0; i < languageNames.Length; i++) { cultureDataSource.Add(languageCodes[i], languageNames[i]); } } else { cultureDataSource = new Dictionary <string, string>(); } return(cultureDataSource); }
public static bool ValidateAppId(string bingAppId) { try { if (string.IsNullOrEmpty(bingAppId)) { return(false); } string[] texts = new string[] { "welcome", "hello" }; TranslatorService.TranslateOptions options = new TranslatorService.TranslateOptions(); TranslatorService.TranslateArrayResponse[] translatedTexts = MTSLanguageServiceClient.TranslateArray(bingAppId, texts, "en", "fr", options); return(true); } catch { } return(false); }
// 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 static TranslatorService.TranslateArrayResponse[] TranslateUsingMTSService(ResourceFile resourceFile, string[] texts, TranslatorService.TranslateOptions options) { TranslatorService.TranslateArrayResponse[] translatedTexts = null; try { using (OperationContextScope scope = new OperationContextScope(Utilities.FormUtility.MTSLanguageServiceClient.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = Utilities.FormUtility.MTSAccessHeader; //Keep appId parameter blank as we are sending access token in authorization header. translatedTexts = Utilities.FormUtility.MTSLanguageServiceClient.TranslateArray("", texts, resourceFile.FromLanguage, resourceFile.ToLanguage, options); } } catch (WebException wex) { LogUtility.WriteError(wex.ToString()); } catch (Exception ex) { LogUtility.WriteError(ex.ToString()); } return(translatedTexts); }
private static TranslatorService.TranslateArrayResponse[] TranslateUsingBingAppId(ResourceFile resourceFile, string[] texts, TranslatorService.TranslateOptions options) { TranslatorService.TranslateArrayResponse[] translatedTexts = Utilities.FormUtility.MTSLanguageServiceClient.TranslateArray(Utilities.FormUtility.BingAppId, texts, resourceFile.FromLanguage, resourceFile.ToLanguage, options); return(translatedTexts); }