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;
}