private void SetLanguages(ComboBox comboBox, string language) { comboBox.Items.Clear(); foreach (var pair in ReversoTranslator.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; }
private void Translate(string source, string target, ReversoTranslator 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) { List <string> result = translator.Translate(source, target, sourceParagraphs, log); 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) { List <string> result = translator.Translate(source, target, sourceParagraphs, log); 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; } }