void PrepareToTranslateArray(TranslateTextArrayEventHandler eventHandler, List <string> keys, List <string> textsToTranslate, string languageFrom, string languageTo) { string body = "<TranslateArrayRequest>" + "<AppId />" + "<From>"; body += languageFrom; body += "</From>" + "<Options>" + " <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">text/plain</ContentType>" + "<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "</Options>" + "<Texts>"; foreach (string text in textsToTranslate) { body += "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">" + text + "</string>"; } body += "</Texts>" + "<To>"; body += languageTo; body += "</To>" + "</TranslateArrayRequest>"; WebRequest translationWebRequest = HttpWebRequest.Create(TranslateArrayURL); translationWebRequest.Headers["Authorization"] = headerValue; translationWebRequest.ContentType = "text/xml"; translationWebRequest.Method = "POST"; using (Stream stream = translationWebRequest.GetRequestStream()) { byte[] arrBytes = System.Text.Encoding.UTF8.GetBytes(body); stream.Write(arrBytes, 0, arrBytes.Length); } TranslateTextArrayData translateTextData = new TranslateTextArrayData(); translateTextData.keys = keys; translateTextData.translationWebRequest = translationWebRequest; translateTextData.eventHandler = eventHandler; translationWebRequest.BeginGetResponse(new AsyncCallback(ReadTranslateArrayResponse), translateTextData); }
void ReadTranslateArrayResponse(IAsyncResult asyncResult) { TranslateTextArrayData translationData = null; HttpWebRequest request = null; HttpWebResponse response = null; try { translationData = asyncResult.AsyncState as TranslateTextArrayData; request = translationData.translationWebRequest as HttpWebRequest; response = request.EndGetResponse(asyncResult) as HttpWebResponse; using (Stream stream = response.GetResponseStream()) { List <string> translatedTexts = new List <string>(); using (XmlReader reader = XmlReader.Create(stream)) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "TranslateArrayResponse") { if (reader.ReadToDescendant("TranslatedText")) { do { translatedTexts.Add(reader.ReadString()); }while (reader.ReadToNextSibling("TranslatedText")); } } } } } Debug.Log("Successfully translated keys!"); if (translationData.eventHandler != null) { translationData.eventHandler(true, translationData.keys, translatedTexts); } translationData.eventHandler = null; } } catch (WebException exception) { Debug.LogError("Failed to translate text array! error: " + exception.Message); using (Stream streamResponse = exception.Response.GetResponseStream()) { using (StreamReader reader = new StreamReader(streamResponse)) { Debug.LogError(reader.ReadToEnd()); } } if (translationData.eventHandler != null) { translationData.eventHandler(false, null, null); } translationData.eventHandler = null; } finally { if (response != null) { response.Close(); response = null; } } }