public translated(string translation, string to, int numOfWords) { value = translation; lang = to; words = new word[numOfWords]; setLangName(to); }
//POST receives the 'untranslated' from client, returns a translated object public translated Post([FromBody]translated u) { AdmAccessToken admToken = admAuth.GetAccessToken(); string text = u.value; string from = "en"; string to = u.lang; string translation; string tempTranslation; string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to; string authToken = "Bearer " + admToken.access_token; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Headers.Add("Authorization", authToken); WebResponse response = null; try { response = httpWebRequest.GetResponse(); using (Stream stream = response.GetResponseStream()) { System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String")); translation = (string)dcs.ReadObject(stream); } } catch (InvalidCastException e) { Console.WriteLine(e.ToString()); translation = "not found"; } //Translate each word back into english, reveals the broken down structure of each language. int numOfWords = translation.Split(' ').Length; translated t = new translated(translation, to, numOfWords); string[] words = translation.Split(' '); for (int i = 0; i < words.Length; i++) { word temp; uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(words[i]) + "&from=" + to + "&to=" + from; //swapped b.c translating back httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Headers.Add("Authorization", authToken); response = null; try { response = httpWebRequest.GetResponse(); using (Stream stream = response.GetResponseStream()) { System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String")); tempTranslation = (string)dcs.ReadObject(stream); } } catch (InvalidCastException e) {tempTranslation = "not found: "+e.ToString();} temp = new word(words[i], tempTranslation); t.words[i] = temp; } return t; }