コード例 #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            Application.DoEvents();

            String currentLanguage = LanguageUtils.GetCurrentLanguage();

            var languageDictFileFormat = Path.Combine(inputFolder, "{0}.dict.txt");

            var languageDictFile = String.Format(languageDictFileFormat, currentLanguage);
            if (!File.Exists(languageDictFile))
            {
                MessageBox.Show($"语言字典文件[{currentLanguage + ".dict.txt"}]不存在!");
                return;
            }
            String[] srcWords = File.ReadAllLines(languageDictFile);

            List<String> list = new List<string>();
            foreach (ListViewItem lvi in lvLanguages.CheckedItems)
            {
                list.Add(lvi.Tag.ToString());
            }
            var translateTarget = list.ToArray();

            Level2ProgressForm progressForm = new Level2ProgressForm();
            progressForm.Title = $"正在使用[{translator.Name}]翻译语言字典...";
            progressForm.Level1Title = "词条";
            progressForm.Level2Title = "目标语言";
            progressForm.Show();
            Application.DoEvents();
            this.Hide();

            progressForm.Level2Count = translateTarget.Length;
            for (int i = 0; i < translateTarget.Length; i++)
            {
                var toLangauge = translateTarget[i];
                var desWords = new String[srcWords.Length];
                progressForm.Level2Index = i;

                progressForm.Level1Index = 0;
                progressForm.Level1Count = srcWords.Length;
                Application.DoEvents();

                for (int j = 0; j < srcWords.Length; j++)
                {
                    var srcWord = srcWords[j];
                    var desWord = (String)null;
                    //最多重试三次
                    for (var retry = 0; retry < 3; retry++)
                    {
                        desWord = translator.Translate(currentLanguage, toLangauge, srcWord);
                        //如果翻译失败,则休息5秒
                        if (desWord == null)
                            Thread.Sleep(5 * 1000);
                        else
                            break;
                    }
                    if (desWord == null)
                    {
                        MessageBox.Show("重试3次,仍然翻译失败!请确保网络连接正常后再试。");
                        progressForm.Close();
                        this.Enabled = true;
                        this.Show();
                        return;
                    }
                    desWords[j] = desWord;
                    progressForm.Level1Index = j;
                    Application.DoEvents();
                }
                File.WriteAllLines(String.Format(languageDictFileFormat, toLangauge), desWords);
            }
            progressForm.Close();
            this.Enabled = true;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
コード例 #2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            Application.DoEvents();

            String currentLanguage = LanguageUtils.GetCurrentLanguage();

            var languageDictFileFormat = Path.Combine(inputFolder, "{0}.dict.txt");

            var languageDictFile = String.Format(languageDictFileFormat, currentLanguage);

            if (!File.Exists(languageDictFile))
            {
                MessageBox.Show($"语言字典文件[{currentLanguage + ".dict.txt"}]不存在!");
                return;
            }
            String[] srcWords = File.ReadAllLines(languageDictFile);

            List <String> list = new List <string>();

            foreach (ListViewItem lvi in lvLanguages.CheckedItems)
            {
                list.Add(lvi.Tag.ToString());
            }
            var translateTarget = list.ToArray();

            Level2ProgressForm progressForm = new Level2ProgressForm();

            progressForm.Title       = $"正在使用[{translator.Name}]翻译语言字典...";
            progressForm.Level1Title = "词条";
            progressForm.Level2Title = "目标语言";
            progressForm.Show();
            Application.DoEvents();
            this.Hide();

            progressForm.Level2Count = translateTarget.Length;
            for (int i = 0; i < translateTarget.Length; i++)
            {
                var toLangauge = translateTarget[i];
                var desWords   = new String[srcWords.Length];
                progressForm.Level2Index = i;

                progressForm.Level1Index = 0;
                progressForm.Level1Count = srcWords.Length;
                Application.DoEvents();

                for (int j = 0; j < srcWords.Length; j++)
                {
                    var srcWord = srcWords[j];
                    var desWord = (String)null;
                    //最多重试三次
                    for (var retry = 0; retry < 3; retry++)
                    {
                        desWord = translator.Translate(currentLanguage, toLangauge, srcWord);
                        //如果翻译失败,则休息5秒
                        if (desWord == null)
                        {
                            Thread.Sleep(5 * 1000);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (desWord == null)
                    {
                        MessageBox.Show("重试3次,仍然翻译失败!请确保网络连接正常后再试。");
                        progressForm.Close();
                        this.Enabled = true;
                        this.Show();
                        return;
                    }
                    desWords[j] = desWord;
                    progressForm.Level1Index = j;
                    Application.DoEvents();
                }
                File.WriteAllLines(String.Format(languageDictFileFormat, toLangauge), desWords);
            }
            progressForm.Close();
            this.Enabled      = true;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }