private void Translate() { Cursor = Cursors.WaitCursor; try { string from = (comboBoxFrom.SelectedItem as Nikse.SubtitleEdit.Forms.GoogleTranslate.ComboBoxItem).Value; string to = (comboBoxTo.SelectedItem as Nikse.SubtitleEdit.Forms.GoogleTranslate.ComboBoxItem).Value; string languagePair = from + "|" + to; buttonGoogle.Text = string.Empty; buttonGoogle.Text = Forms.GoogleTranslate.TranslateTextViaApi(textBoxSourceText.Text, languagePair); using (var gt = new GoogleTranslate()) { Subtitle subtitle = new Subtitle(); subtitle.Paragraphs.Add(new Paragraph(0, 0, textBoxSourceText.Text)); gt.Initialize(subtitle, string.Empty, false, System.Text.Encoding.UTF8); from = FixMsLocale(from); to = FixMsLocale(to); gt.DoMicrosoftTranslate(from, to); buttonMicrosoft.Text = gt.TranslatedSubtitle.Paragraphs[0].Text; } } finally { Cursor = Cursors.Default; } }
private void Translate() { Cursor = Cursors.WaitCursor; try { string from = (comboBoxFrom.SelectedItem as GoogleTranslate.ComboBoxItem).Value; string to = (comboBoxTo.SelectedItem as GoogleTranslate.ComboBoxItem).Value; string languagePair = from + "|" + to; buttonGoogle.Text = string.Empty; // google translate bool romanji = languagePair.EndsWith("|romanji", StringComparison.InvariantCulture); if (romanji) { languagePair = from + "|ja"; } var screenScrapingEncoding = GoogleTranslate.GetScreenScrapingEncoding(languagePair); buttonGoogle.Text = GoogleTranslate.TranslateTextViaScreenScraping(textBoxSourceText.Text, languagePair, screenScrapingEncoding, romanji); // ms translator if (!string.IsNullOrEmpty(Configuration.Settings.Tools.MicrosoftTranslatorApiKey)) { var translator = new MicrosoftTranslator(Configuration.Settings.Tools.MicrosoftTranslatorApiKey); var result = translator.Translate(from, to, new List <string> { textBoxSourceText.Text }, new StringBuilder()); buttonMicrosoft.Text = result[0]; } else { using (var gt = new GoogleTranslate()) { var subtitle = new Subtitle(); subtitle.Paragraphs.Add(new Paragraph(0, 0, textBoxSourceText.Text)); gt.Initialize(subtitle, string.Empty, false, Encoding.UTF8); from = FixMsLocale(from); to = FixMsLocale(to); gt.DoMicrosoftTranslate(from, to); buttonMicrosoft.Text = gt.TranslatedSubtitle.Paragraphs[0].Text; } } } finally { Cursor = Cursors.Default; } }
private void Translate() { Cursor = Cursors.WaitCursor; try { string from = ((GoogleTranslate.ComboBoxItem)comboBoxFrom.SelectedItem).Value; string to = ((GoogleTranslate.ComboBoxItem)comboBoxTo.SelectedItem).Value; buttonGoogle.Text = string.Empty; // google translate buttonGoogle.Text = new GoogleTranslator1().Translate(from, to, new List <Paragraph> { new Paragraph { Text = textBoxSourceText.Text } }, new StringBuilder()).FirstOrDefault(); // ms translator if (!string.IsNullOrEmpty(Configuration.Settings.Tools.MicrosoftTranslatorApiKey)) { var translator = new MicrosoftTranslator(Configuration.Settings.Tools.MicrosoftTranslatorApiKey); var result = translator.Translate(from, to, new List <Paragraph> { new Paragraph { Text = textBoxSourceText.Text } }, new StringBuilder()); buttonMicrosoft.Text = result[0]; } else { using (var gt = new GoogleTranslate()) { var subtitle = new Subtitle(); subtitle.Paragraphs.Add(new Paragraph(0, 0, textBoxSourceText.Text)); gt.Initialize(subtitle, string.Empty, false, Encoding.UTF8); from = FixMsLocale(from); to = FixMsLocale(to); gt.DoMicrosoftTranslate(from, to); buttonMicrosoft.Text = gt.TranslatedSubtitle.Paragraphs[0].Text; } } } finally { Cursor = Cursors.Default; } }
private void Translate() { Cursor = Cursors.WaitCursor; try { string from = (comboBoxFrom.SelectedItem as GoogleTranslate.ComboBoxItem).Value; string to = (comboBoxTo.SelectedItem as GoogleTranslate.ComboBoxItem).Value; string languagePair = from + "|" + to; buttonGoogle.Text = string.Empty; // google translate bool romanji = languagePair.EndsWith("|romanji", StringComparison.InvariantCulture); if (romanji) { languagePair = from + "|ja"; } var screenScrapingEncoding = GoogleTranslate.GetScreenScrapingEncoding(languagePair); buttonGoogle.Text = GoogleTranslate.TranslateTextViaScreenScraping(textBoxSourceText.Text, languagePair, screenScrapingEncoding, romanji); using (var gt = new GoogleTranslate()) { Subtitle subtitle = new Subtitle(); subtitle.Paragraphs.Add(new Paragraph(0, 0, textBoxSourceText.Text)); gt.Initialize(subtitle, string.Empty, false, System.Text.Encoding.UTF8); from = FixMsLocale(from); to = FixMsLocale(to); gt.DoMicrosoftTranslate(from, to); buttonMicrosoft.Text = gt.TranslatedSubtitle.Paragraphs[0].Text; } } finally { Cursor = Cursors.Default; } }
private void TranslateViaGoogle(bool onlySelectedLines, bool useGoogle) { if (!IsSubtitleLoaded) { DisplaySubtitleNotLoadedMessage(); return; } bool isAlternateVisible = SubtitleListview1.IsAlternateTextColumnVisible; ReloadFromSourceView(); using (var googleTranslate = new GoogleTranslate()) { SaveSubtitleListviewIndices(); string title = _language.GoogleTranslate; if (!useGoogle) title = _language.MicrosoftTranslate; if (onlySelectedLines) { var selectedLines = new Subtitle { WasLoadedWithFrameNumbers = _subtitle.WasLoadedWithFrameNumbers }; foreach (int index in SubtitleListview1.SelectedIndices) selectedLines.Paragraphs.Add(_subtitle.Paragraphs[index]); title += " - " + _language.SelectedLines; googleTranslate.Initialize(selectedLines, title, useGoogle, GetCurrentEncoding()); } else { googleTranslate.Initialize(_subtitle, title, useGoogle, GetCurrentEncoding()); } if (googleTranslate.ShowDialog(this) == DialogResult.OK) { _subtitleListViewIndex = -1; MakeHistoryForUndo(_language.BeforeGoogleTranslation); if (onlySelectedLines) { // we only update selected lines int i = 0; foreach (int index in SubtitleListview1.SelectedIndices) { _subtitle.Paragraphs[index] = googleTranslate.TranslatedSubtitle.Paragraphs[i]; i++; } ShowStatus(_language.SelectedLinesTranslated); } else { _subtitleAlternate = new Subtitle(_subtitle); _subtitleAlternateFileName = _fileName; _fileName = null; _subtitle.Paragraphs.Clear(); foreach (var p in googleTranslate.TranslatedSubtitle.Paragraphs) _subtitle.Paragraphs.Add(new Paragraph(p)); ShowStatus(_language.SubtitleTranslated); } ShowSource(); if (!onlySelectedLines) { SubtitleListview1.ShowAlternateTextColumn(_languageGeneral.OriginalText); SubtitleListview1.AutoSizeAllColumns(this); SetupAlternateEdit(); } SubtitleListview1.Fill(_subtitle, _subtitleAlternate); ResetHistory(); RestoreSubtitleListviewIndices(); _converted = true; SetTitle(); //if (googleTranslate.ScreenScrapingEncoding != null) // SetEncoding(googleTranslate.ScreenScrapingEncoding); SetEncoding(Encoding.UTF8); if (!isAlternateVisible) { toolStripMenuItemShowOriginalInPreview.Checked = false; Configuration.Settings.General.ShowOriginalAsPreviewIfAvailable = false; audioVisualizer.Invalidate(); } } } }
private void TranslateViaGoogle(bool onlySelectedLines, bool useGoogle) { if (IsSubtitleLoaded) { ReloadFromSourceView(); var googleTranslate = new GoogleTranslate(); _formPositionsAndSizes.SetPositionAndSize(googleTranslate); SaveSubtitleListviewIndexes(); string title = _language.GoogleTranslate; if (!useGoogle) title = _language.MicrosoftTranslate; if (onlySelectedLines) { var selectedLines = new Subtitle { WasLoadedWithFrameNumbers = _subtitle.WasLoadedWithFrameNumbers }; foreach (int index in SubtitleListview1.SelectedIndices) selectedLines.Paragraphs.Add(_subtitle.Paragraphs[index]); title += " - " + _language.SelectedLines; googleTranslate.Initialize(selectedLines, title, useGoogle, GetCurrentEncoding()); } else { googleTranslate.Initialize(_subtitle, title, useGoogle, GetCurrentEncoding()); } if (googleTranslate.ShowDialog(this) == DialogResult.OK) { _subtitleListViewIndex = -1; MakeHistoryForUndo(_language.BeforeGoogleTranslation); if (onlySelectedLines) { // we only update selected lines int i = 0; foreach (int index in SubtitleListview1.SelectedIndices) { _subtitle.Paragraphs[index] = googleTranslate.TranslatedSubtitle.Paragraphs[i]; i++; } ShowStatus(_language.SelectedLinesTranslated); } else { _subtitleAlternate = new Subtitle(_subtitle); _subtitleAlternateFileName = _fileName; _fileName = null; _subtitle.Paragraphs.Clear(); foreach (Paragraph p in googleTranslate.TranslatedSubtitle.Paragraphs) _subtitle.Paragraphs.Add(new Paragraph(p)); ShowStatus(_language.SubtitleTranslated); } ShowSource(); if (!onlySelectedLines) { SubtitleListview1.ShowAlternateTextColumn(Configuration.Settings.Language.General.OriginalText); SubtitleListview1.AutoSizeAllColumns(this); SetupAlternateEdit(); } SubtitleListview1.Fill(_subtitle, _subtitleAlternate); ResetHistory(); RestoreSubtitleListviewIndexes(); _converted = true; SetTitle(); //if (googleTranslate.ScreenScrapingEncoding != null) // SetEncoding(googleTranslate.ScreenScrapingEncoding); SetEncoding(Encoding.UTF8); } _formPositionsAndSizes.SavePositionAndSize(googleTranslate); } else { MessageBox.Show(_language.NoSubtitleLoaded, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void Translate() { Cursor = Cursors.WaitCursor; try { string from = (comboBoxFrom.SelectedItem as GoogleTranslate.ComboBoxItem).Value; string to = (comboBoxTo.SelectedItem as GoogleTranslate.ComboBoxItem).Value; string languagePair = from + "|" + to; buttonGoogle.Text = string.Empty; // google translate bool romanji = languagePair.EndsWith("|romanji", StringComparison.InvariantCulture); if (romanji) languagePair = from + "|ja"; var screenScrapingEncoding = GoogleTranslate.GetScreenScrapingEncoding(languagePair); buttonGoogle.Text = GoogleTranslate.TranslateTextViaScreenScraping(textBoxSourceText.Text, languagePair, screenScrapingEncoding, romanji); using (var gt = new GoogleTranslate()) { Subtitle subtitle = new Subtitle(); subtitle.Paragraphs.Add(new Paragraph(0, 0, textBoxSourceText.Text)); gt.Initialize(subtitle, string.Empty, false, System.Text.Encoding.UTF8); from = FixMsLocale(from); to = FixMsLocale(to); gt.DoMicrosoftTranslate(from, to); buttonMicrosoft.Text = gt.TranslatedSubtitle.Paragraphs[0].Text; } } finally { Cursor = Cursors.Default; } }