public List <string> GetTranslation(LanguagePair languageDirection, List <string> sourceString) { var targetLanguage = languageDirection.TargetCulture.TwoLetterISOLanguageName; try { var client = new HttpClient(); var content = new BatchInput { text = sourceString }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(content); var response = client.PostAsync(_url + "?auth=" + _auth + "&olang=" + targetLanguage + "&odomain=" + _domain, new StringContent(json, Encoding.UTF8, "application/json")).Result; var translatedObject = new JavaScriptSerializer().Deserialize <JSONResponseBatch>(response.Content.ReadAsStringAsync().Result); var translatedText = translatedObject.result; return(translatedText); } catch (Exception ex) { if (!(ex.InnerException is HttpRequestException) || !(ex.InnerException.InnerException is WebException response)) { return(null); } var message = $"HTTP error message: {response.Message}"; throw new Exception(message); } }
public List <string> GetTranslation(LanguagePair languageDirection, List <string> sourceString) { var sourceLanguage = languageDirection.SourceCulture.TwoLetterISOLanguageName; var targetLanguage = languageDirection.TargetCulture.TwoLetterISOLanguageName; try { var client = new HttpClient(); client.DefaultRequestHeaders.Add("x-api-key", _auth); client.DefaultRequestHeaders.Add("application", "SDL"); var content = new BatchInput { text = sourceString, src = sourceLanguage, tgt = targetLanguage, domain = _domain }; var json = Newtonsoft.Json.JsonConvert.SerializeObject(content); var strContent = new StringContent(json, Encoding.UTF8, "application/json"); var response = client.PostAsync(_url, strContent).Result; var jsonResponse = new JavaScriptSerializer().Deserialize <JSONResponseBatch>(response.Content.ReadAsStringAsync().Result); return(jsonResponse.result); } catch (Exception ex) { if (!(ex.InnerException is HttpRequestException) || !(ex.InnerException.InnerException is WebException response)) { return(null); } var message = $"HTTP error message: {response.Message}"; throw new Exception(message); } }
/// <summary> /// Translates multiple strings with the help of the dummy MT service. /// </summary> /// <param name="tokenCode">The token code.</param> /// <param name="input">The strings to translate.</param> /// <param name="srcLangCode">The source language code.</param> /// <param name="trgLangCode">The target language code.</param> /// <returns>The translated strings.</returns> public static List <String> BatchTranslate(TartuNLPOptions options, List <string> input, string srcLangCode, string trgLangCode) { if (trgLangCode.Contains("-")) { trgLangCode = trgLangCode.Split('-')[0]; } HttpClient client = new HttpClient(); var content = new BatchInput(); content.text = input; var json = Newtonsoft.Json.JsonConvert.SerializeObject(content); //var content = "{'text':" + Newtonsoft.Json.JsonConvert.SerializeObject(input) + "}"; HttpResponseMessage response = client.PostAsync(options.SecureSettings.URL + "?auth=" + options.SecureSettings.Auth + "&olang=" + trgLangCode + "&odomain=" + options.GeneralSettings.SelectedDomain, new StringContent(json, Encoding.UTF8, "application/json")).Result; string test = response.Content.ReadAsStringAsync().Result; var JSONResponse = new JavaScriptSerializer().Deserialize <JSONResponseBatch>(response.Content.ReadAsStringAsync().Result); return(JSONResponse.result); }