Пример #1
0
 /// <summary>
 /// Ensures the translator has been initialized.
 /// </summary>
 private static void EnsureInitialized()
 {
     if (GoogleTranslator._languageModeMap == null)
     {
         _languageModeMap = Translator.Languages("Google");
     }
 }
Пример #2
0
        public override string Translate(string text)
        {
            //System.Object[] body = new System.Object[] { new { text = Uri.EscapeUriString(text) } };
            //var requestBody = JsonConvert.SerializeObject(body);
            var langs = Translator.Languages("Yandex");

            langs.TryGetValue(SourceLanguage, out string from);
            langs.TryGetValue(TargetLanguage, out string to);
            string lang = from + "-" + to;

            using (var client = new HttpClient())
                using (var request = new HttpRequestMessage())
                {
                    request.Method     = HttpMethod.Post;
                    request.RequestUri = new Uri(host + path + "&lang=" + lang);
                    request.Content    = new StringContent("text=" + Uri.EscapeUriString(text), Encoding.UTF8, "application/x-www-form-urlencoded");

                    var response     = client.SendAsync(request).Result;
                    var responseBody = response.Content.ReadAsStringAsync().Result;

                    Console.WriteLine("YT: got response: " + responseBody);
                    var jobj = JObject.Parse(responseBody);
                    if (jobj["code"].ToObject <int>() != 200)
                    {
                        throw new Exception("Yandex API Error.\nCode: " + jobj["code"] + "\nMessage: " + jobj["message"]);
                        //Console.WriteLine("YT: error in responce, code: " + jobj["code"].ToObject<int>());
                        //Console.WriteLine("YT: error in responce, message: " + jobj["message"].ToString());
                        //return "";
                    }
                    else
                    {
                        return(jobj["text"][0].ToString());
                    }
                }
        }
        private string GetURI()
        {
            var langs = Translator.Languages("Microsoft");

            langs.TryGetValue(SourceLanguage, out string from);
            langs.TryGetValue(TargetLanguage, out string to);
            return(host + path + "&from=" + from + "&to=" + to);
        }
Пример #4
0
        // Fill target language combo box
        private void UpdateTargCB()
        {
            targetLanguageCB.Items.Clear();

            targetLanguageCB.Items.Add(new ComboBoxItem()
            {
                Content    = "Select Target Language",
                Visibility = Visibility.Collapsed
            });
            targetLanguageCB.SelectedIndex = 0;

            //switch (pref.Get("translator"))
            //{
            //    case "Google":
            //        foreach (string lang in GoogleTranslator.Langs)
            //        {
            //            targetLanguageCB.Items.Add(lang);
            //        }
            //        break;
            //    case "Microsoft":
            //        foreach (string lang in MicrosoftTranslator.Languages.Keys)
            //        {
            //            targetLanguageCB.Items.Add(lang);
            //        }
            //        break;
            //    case "Yandex":
            //        foreach (string lang in YandexTranslator.Languages.Keys)
            //        {
            //            targetLanguageCB.Items.Add(lang);
            //        }
            //        break;
            //    default:
            //        throw new ArgumentException("Unknown translator: " + pref.Get("translator"));
            //}
            foreach (string lang in Translator.Languages(pref.Get("translator")).Keys)
            {
                targetLanguageCB.Items.Add(lang);
            }
            targetLanguageCB.SelectedIndex = 0;
        }