Convert type of TranslateArrayResponse to string[]

139 views Asked by At

I am working with Microsoft Translator (http://msdn.microsoft.com/en-us/library/ff512422.aspx). Specifically the TranslateArray() method which basically takes a string array of texts to translate and the result is an array with the translated text.

The resulting translation comes with a type of TranslateArrayResponse[]. My code looks something like this (shortened down for clarity)

string[] sourceTranslate = new string[3] {"My name is Peter", "Her name is Suzan", "We have fun"};
....
TranslateArrayResponse[] result = client.TranslateArray("", sourceTranslate, "en", "de", options);

The challenge with the client.TranslateArray() method is that it only allows each request to be 10,000 characters and number items a maximum of 2000. My sourceTranslate (a resx file) easily contains more than 10,000 characters, so to get around this I am splitting up requests to fit these boundaries.

My question is how do I copy the value result to a string[] or similar - I need to work later with the result (i.e. saving the results back to a new resx file)?

Obviously stuff like result += ... won't work.

1

There are 1 answers

1
O. R. Mapper On BEST ANSWER

There are various ways to do this.

A straightforward one is to create a new string step by step while looping over the array of TranslateArrayResponse values - according to the docs, each of them has a TranslatedText property of type string:

StringBuilder sb = new StringBuilder();
foreach (var tar in result) {
    sb.Append(tar.TranslatedText);
}
string resultString = sb.ToString();

Another solution is to use the LINQ Select method to extract the translated text from each item and then the string.Join method to concatenate the result enumeration of strings:

string resultString = string.Join("", result.Select(r => r.TranslatedText));