private void bTranslate_Click(object sender, EventArgs e) { Translator t = new Translator(); var bindingSource = new BindingSource(); pBar.Maximum = obj.List.Count; pBar.Value = 1; pBar.Step = 1; for (int i = 0; i < obj.List.Count; i++) { SimpleData temp = (SimpleData)obj.List[i]; if (chkIsEmpty.Checked && temp.Vi == string.Empty) { temp = TranslateLang(t, temp); temp.Chon = true; } else if(chkFull.Checked) { temp = TranslateLang(t, temp); } bindingSource.Add(temp); pBar.PerformStep(); pBar.Update(); } gvData.DataSource = bindingSource; obj = bindingSource; gvData.Update(); }
/// <summary> /// Handles the Click event of the _btnTranslate control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void _btnTranslate_Click(object sender, EventArgs e) { // Initialize the translator Translator t = new Translator(); t.SourceLanguage = (string) this._comboFrom.SelectedItem; t.TargetLanguage = (string) this._comboTo.SelectedItem; t.SourceText = this._editSourceText.Text; this._editTarget.Text = string.Empty; this._editTarget.Update(); this._editReverseTranslation.Text = string.Empty; this._editReverseTranslation.Update(); // Translate the text try { // Forward translation this.Cursor = Cursors.WaitCursor; this._lblStatus.Text = "Translating..."; this._lblStatus.Update(); t.Translate(); this._editTarget.Text = t.Translation; this._editTarget.Update(); // Reverse translation this._lblStatus.Text = "Reverse translating..."; this._lblStatus.Update(); Thread.Sleep (500); // let Google breathe t.SourceLanguage = (string) this._comboTo.SelectedItem; t.TargetLanguage = (string) this._comboFrom.SelectedItem; t.SourceText = this._editTarget.Text; t.Translate(); this._editReverseTranslation.Text = t.Translation; } catch (Exception ex) { MessageBox.Show (ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { this._lblStatus.Text = string.Empty; this.Cursor = Cursors.Default; } }
/// <summary> /// Translates the specified source text. /// </summary> /// <param name="sourceText">The source text.</param> /// <param name="sourceLanguage">The source language.</param> /// <param name="targetLanguage">The target language.</param> /// <returns>The translation.</returns> public string Translate(string sourceText, string sourceLanguage, string targetLanguage) { // Initialize this.Error = null; this.TranslationSpeechUrl = null; this.TranslationTime = TimeSpan.Zero; DateTime tmStart = DateTime.Now; string translation = string.Empty; try { // Download translation string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}", Translator.LanguageEnumToIdentifier(sourceLanguage), Translator.LanguageEnumToIdentifier(targetLanguage), HttpUtility.UrlEncode(sourceText)); string outputFile = Path.GetTempFileName(); using (WebClient wc = new WebClient()) { wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); wc.DownloadFile(url, outputFile); } // Get translated text if (File.Exists(outputFile)) { // Get phrase collection string text = File.ReadAllText(outputFile); int index = text.IndexOf(string.Format(",,\"{0}\"", Translator.LanguageEnumToIdentifier(sourceLanguage))); if (index == -1) { // Translation of single word int startQuote = text.IndexOf('\"'); if (startQuote != -1) { int endQuote = text.IndexOf('\"', startQuote + 1); if (endQuote != -1) { translation = text.Substring(startQuote + 1, endQuote - startQuote - 1); } } } else { // Translation of phrase text = text.Substring(0, index); text = text.Replace("],[", ","); text = text.Replace("]", string.Empty); text = text.Replace("[", string.Empty); text = text.Replace("\",\"", "\""); // Get translated phrases string[] phrases = text.Split(new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; (i < phrases.Count()); i += 2) { string translatedPhrase = phrases[i]; if (translatedPhrase.StartsWith(",,")) { i--; continue; } translation += translatedPhrase + " "; } } // Fix up translation translation = translation.Trim(); translation = translation.Replace(" ?", "?"); translation = translation.Replace(" !", "!"); translation = translation.Replace(" ,", ","); translation = translation.Replace(" .", "."); translation = translation.Replace(" ;", ";"); // And translation speech URL this.TranslationSpeechUrl = string.Format("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx", HttpUtility.UrlEncode(translation), Translator.LanguageEnumToIdentifier(targetLanguage), translation.Length); } } catch (Exception ex) { this.Error = ex; } // Return result this.TranslationTime = DateTime.Now - tmStart; return(translation); }
private SimpleData TranslateLang(Translator t, SimpleData temp) { t.SourceLanguage = (string)this._comboFrom.SelectedItem; t.TargetLanguage = (string)this._comboTo.SelectedItem; t.SourceText = temp.En; try { // Forward translation this.Cursor = Cursors.WaitCursor; this._lblStatus.Text = "Being translation..." + temp.KhoaChinh + "...Key"; this._lblStatus.Update(); t.Translate(); var stext = t.Translation; stext = stext.Replace(">", ">"); if (stext.StartsWith("gt;")) { stext = stext.Replace("gt;", ">"); } stext = stext.Replace("<", "<"); if (stext.StartsWith("lt;")) { stext = stext.Replace("<", "<"); } stext = stext.Replace("% 1 $ s", " %1$s "); stext = stext.Replace("% 2 $ s", " %2$s "); stext = stext.Replace("% 3 $ s", " %3$s "); stext = stext.Replace("% 4 $ s", " %4$s "); stext = stext.Replace("% 5 $ s", " %5$s "); stext = stext.Replace("% 6 $ s", " %6$s "); stext = stext.Replace("% d", " %d "); stext = stext.Replace("% s", " %s "); if (stext.StartsWith("D ")) { stext = stext.Replace("D ", "%d "); } if (stext.StartsWith("S ")) { stext = stext.Replace("S ", "%s "); } stext = stext.Replace("_QQ_", " \"_QQ_\" "); temp.Vi = stext; this._lblStatus.Text = "Translate completed..." + temp.KhoaChinh + "...Key"; this._lblStatus.Update(); } catch (Exception ex) { MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { this._lblStatus.Text = string.Empty; this.Cursor = Cursors.Default; } return temp; }