示例#1
0
        private Subtitle TranslateSub(Subtitle subtitleToTranslate, string languagePair)
        {
            Subtitle otherTranslated = new Subtitle(subtitleToTranslate, true);

            Parallel.For(0, otherTranslated.Paragraphs.Count, i =>
            {
                otherTranslated.Paragraphs[i].Text = GoogleTranslate.TranslateTextViaScreenScraping(subtitleToTranslate.Paragraphs[i].Text, languagePair, Encoding.Default, false);
            });
            return(otherTranslated);
        }
        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         = (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;
            }
        }