Exemple #1
0
        public GoogleTranslateInfo TranslateText(string text, string targetLanguage, string sourceLanguage = null)
        {
            GoogleTranslateInfo translateInfo = new GoogleTranslateInfo();
            translateInfo.Text = text;
            translateInfo.SourceLanguage = sourceLanguage;
            translateInfo.TargetLanguage = targetLanguage;

            Dictionary<string, string> args = new Dictionary<string, string>();
            args.Add("key", APIKey);
            if (!string.IsNullOrEmpty(sourceLanguage)) args.Add("source", sourceLanguage);
            args.Add("target", targetLanguage);
            args.Add("q", text);

            string response = SendRequest(HttpMethod.GET, APIURL, args);

            if (!string.IsNullOrEmpty(response))
            {
                try
                {
                    JToken jt = JObject.Parse(response).SelectToken("data.translations");
                    translateInfo.Result = (string)jt[0]["translatedText"];
                    if (string.IsNullOrEmpty(sourceLanguage))
                    {
                        translateInfo.SourceLanguage = (string)jt[0]["detectedSourceLanguage"];
                    }
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e);
                }
            }

            return translateInfo;
        }
        private void UpdateGoogleTranslateGUI(GoogleTranslateInfo info)
        {
            btnTranslate.Enabled = true;
            btnTranslateTo.Enabled = true;

            txtTranslateText.Text = info.Text;
            txtLanguages.Text = info.SourceLanguage + " -> " + info.TargetLanguage;
            txtTranslateResult.Text = info.Result;
        }
Exemple #3
0
        public GoogleTranslateInfo TranslateText(string text, string targetLanguage, string sourceLanguage = null)
        {
            GoogleTranslateInfo translateInfo = new GoogleTranslateInfo();

            translateInfo.Text           = text;
            translateInfo.SourceLanguage = sourceLanguage;
            translateInfo.TargetLanguage = targetLanguage;

            Dictionary <string, string> args = new Dictionary <string, string>();

            args.Add("key", APIKey);
            if (!string.IsNullOrEmpty(sourceLanguage))
            {
                args.Add("source", sourceLanguage);
            }
            args.Add("target", targetLanguage);
            args.Add("q", text);

            string response = SendRequest(HttpMethod.GET, APIURL, args);

            if (!string.IsNullOrEmpty(response))
            {
                try
                {
                    JToken jt = JObject.Parse(response).SelectToken("data.translations");
                    translateInfo.Result = (string)jt[0]["translatedText"];
                    if (string.IsNullOrEmpty(sourceLanguage))
                    {
                        translateInfo.SourceLanguage = (string)jt[0]["detectedSourceLanguage"];
                    }
                }
                catch (Exception e)
                {
                    DebugHelper.WriteException(e);
                }
            }

            return(translateInfo);
        }
 public void StartBW_LanguageTranslator(GoogleTranslateInfo gti)
 {
     btnTranslate.Enabled = false;
     btnTranslateTo.Enabled = false;
     CreateWorker().RunWorkerAsync(gti);
 }
Exemple #5
0
 public GoogleTranslateInfo TranslateText(GoogleTranslateInfo info)
 {
     return(TranslateText(info.Text, info.TargetLanguage, info.SourceLanguage));
 }
Exemple #6
0
 public GoogleTranslateInfo TranslateText(GoogleTranslateInfo info)
 {
     return TranslateText(info.Text, info.TargetLanguage, info.SourceLanguage);
 }
        public void TranslateAsync(GoogleTranslateInfo info)
        {
            btnTranslate.Enabled = false;
            btnTranslateTo.Enabled = false;

            TaskEx.Run(() =>
            {
                info = new GoogleTranslate(Config.APIKey).TranslateText(info);
            },
            () =>
            {
                UpdateGoogleTranslateGUI(info);
            });
        }
 private void UpdateGoogleTranslateGUI(GoogleTranslateInfo info)
 {
     Loader.MyGTGUI.txtTranslateText.Text = info.Text;
     Loader.MyGTGUI.txtLanguages.Text = info.SourceLanguage + " -> " + info.TargetLanguage;
     Loader.MyGTGUI.txtTranslateResult.Text = info.Result;
 }
 public void RunWorkerAsync_LanguageTranslator(GoogleTranslateInfo translationInfo)
 {
     WorkerTask gtTask = CreateTask(WorkerTask.JobLevel2.Translate);
     if (Loader.MyGTGUI == null)
     {
         Loader.MyGTGUI = new GoogleTranslateGUI(Engine.ConfigGT);
     }
     Loader.MyGTGUI.btnTranslate.Enabled = false;
     Loader.MyGTGUI.btnTranslateTo.Enabled = false;
     gtTask.SetTranslationInfo(translationInfo);
     gtTask.RunWorker();
 }
 public void SetTranslationInfo(GoogleTranslateInfo gti)
 {
     Job1 = EDataType.Text;
     TranslationInfo = gti;
 }