Пример #1
0
        private void SetLanguages(ComboBox comboBox, string language)
        {
            comboBox.Items.Clear();
            foreach (var pair in BingTranslateScreenScraper.GetTranslationPairs())
            {
                comboBox.Items.Add(pair);
            }
            int i = 0;

            foreach (var l in comboBox.Items)
            {
                if (l is TranslationPair tl && tl.Code.Equals(language, StringComparison.OrdinalIgnoreCase))
                {
                    comboBox.SelectedIndex = i;
                    return;
                }
                i++;
            }
            comboBox.SelectedIndex = 0;
        }
Пример #2
0
        private void Translate(string source, string target, BingTranslateScreenScraper translator, int maxTextSize, int maximumRequestArrayLength = 100)
        {
            buttonOk.Enabled     = false;
            buttonCancel.Enabled = false;
            _abort               = false;
            Cursor.Current       = Cursors.WaitCursor;
            progressBar1.Maximum = _subtitleOriginal.Paragraphs.Count;
            progressBar1.Value   = 0;
            progressBar1.Visible = true;
            var sourceParagraphs = new List <Paragraph>();

            try
            {
                var log           = new StringBuilder();
                var sourceLength  = 0;
                var selectedItems = listView1.SelectedItems;
                var startIndex    = selectedItems.Count <= 0 ? 0 : selectedItems[0].Index;
                var start         = startIndex;
                int index         = startIndex;
                for (int i = startIndex; i < _subtitleOriginal.Paragraphs.Count; i++)
                {
                    Paragraph p = _subtitleOriginal.Paragraphs[i];
                    sourceLength += Uri.EscapeDataString(p.Text).Length;
                    if ((sourceLength >= maxTextSize || sourceParagraphs.Count >= maximumRequestArrayLength) && sourceParagraphs.Count > 0)
                    {
                        translator.Translate(source, target, sourceParagraphs, log);
                        List <string> result = null;
                        var           T      = DateTime.UtcNow;
                        while (result == null)
                        {
                            result = translator.GetTranslationResult(target, sourceParagraphs);
                            Thread.Sleep(10);
                            Application.DoEvents();
                            var seconds = (DateTime.UtcNow - T).TotalSeconds;
                            if (seconds > 15 && result == null || _abort)
                            {
                                log.AppendLine("No response from webview!" + Environment.NewLine);
                                result = new List <string>();
                            }
                        }

                        textBoxLog.Text = log.ToString().Trim();
                        if (log.Length > 1000000)
                        {
                            log.Clear();
                        }
                        FillTranslatedText(result, start, index - 1);
                        sourceLength = 0;
                        sourceParagraphs.Clear();
                        progressBar1.Refresh();
                        Application.DoEvents();
                        start = index;
                    }
                    sourceParagraphs.Add(p);
                    index++;
                    progressBar1.Value = index;
                    if (_abort)
                    {
                        break;
                    }
                }

                if (sourceParagraphs.Count > 0)
                {
                    translator.Translate(source, target, sourceParagraphs, log);
                    List <string> result = null;
                    var           T      = DateTime.UtcNow;
                    while (result == null)
                    {
                        result = translator.GetTranslationResult(target, sourceParagraphs);
                        Application.DoEvents();
                        var seconds = (DateTime.UtcNow - T).TotalSeconds;
                        if (seconds > 15 || _abort)
                        {
                            log.AppendLine("No response from webview!" + Environment.NewLine);
                            result = new List <string>();
                        }
                    }
                    textBoxLog.Text = log.ToString().Trim();
                    FillTranslatedText(result, start, index - 1);
                }
            }
            catch (WebException webException)
            {
                MessageBox.Show(webException.Source + ": " + webException.Message);
            }
            finally
            {
                progressBar1.Visible    = false;
                Cursor.Current          = Cursors.Default;
                buttonTranslate.Enabled = true;
                buttonOk.Enabled        = true;
                buttonCancel.Enabled    = true;
            }
        }