private void cbTranslator_SelectedIndexChanged(object sender, EventArgs e)
        {
            translator = (ITranslator)cbTranslator.SelectedItem;
            lvLanguages.Items.Clear();
            String currentLanguage = LanguageUtils.GetCurrentLanguage();

            foreach (String language in translator.GetSupportLanguages())
            {
                //跳过当前语言
                if (language == currentLanguage)
                {
                    continue;
                }
                CultureInfo cultureInfo = new CultureInfo(language);
                var         lvi         = lvLanguages.Items.Add(cultureInfo.DisplayName);
                lvi.Tag = language;
            }
        }
        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();
        }
Beispiel #3
0
        private void GenerateLanguageResourceForm_Load(object sender, EventArgs e)
        {
            var currentLanguage = LanguageUtils.GetCurrentLanguage();
            //语言目录
            DirectoryInfo languageFolder = new DirectoryInfo(inputFolder);
            //取出所有的语言资源文件
            DirectoryInfo srcLanguageResourceFolder = new DirectoryInfo(Path.Combine(inputFolder, currentLanguage));
            var           srcLanguageResourceFiles  = srcLanguageResourceFolder.GetFiles("*.txt", SearchOption.AllDirectories);
            //取出当前语言的字典文件的全部条目
            var srcLanguageDictLines = File.ReadAllLines(Path.Combine(inputFolder, $"{currentLanguage}.dict.txt"));

            //开始生成
            var dictFiles = languageFolder.GetFiles("*.dict.txt").Where(t => !t.Name.StartsWith(currentLanguage + ".")).ToArray();

            base.Level2Count = dictFiles.Length;
            for (var i = 0; i < dictFiles.Length; i++)
            {
                base.Level2Index = i;
                var dictFile             = dictFiles[i];
                var desLanguage          = dictFile.Name.Replace(".dict.txt", "");
                var desLanguageDictLines = File.ReadAllLines(dictFile.FullName);
                //语言替换对应字典
                Dictionary <String, String> languageDict = new Dictionary <string, string>();
                for (var x = 0; x < srcLanguageDictLines.Length; x++)
                {
                    languageDict[srcLanguageDictLines[x]] = desLanguageDictLines[x];
                }
                //生成语言资源文件
                base.Level1Count = srcLanguageResourceFiles.Length;
                for (var j = 0; j < srcLanguageResourceFiles.Length; j++)
                {
                    var srcLanguageResourceFile = srcLanguageResourceFiles[j];
                    var content     = File.ReadAllText(srcLanguageResourceFile.FullName);
                    var contentDict = LanguageUtils.GetLanguageResourceDictionary(content);
                    foreach (var key in contentDict.Keys.ToArray())
                    {
                        var value = contentDict[key];
                        if (!languageDict.ContainsKey(value))
                        {
                            continue;
                        }
                        contentDict[key] = languageDict[value];
                    }
                    var desContent = LanguageUtils.GetToWriteLanguageText(contentDict);
                    var desLanguageResourceFile = Path.Combine(inputFolder, desLanguage, srcLanguageResourceFile.FullName.Substring(srcLanguageResourceFolder.FullName.Length + 1));
                    //如果目录不存在,则创建目录
                    var desLanguageResourceFileFolder = Path.GetDirectoryName(desLanguageResourceFile);
                    if (!Directory.Exists(desLanguageResourceFileFolder))
                    {
                        Directory.CreateDirectory(desLanguageResourceFileFolder);
                    }
                    //保存文件
                    File.WriteAllText(desLanguageResourceFile, desContent);
                    //更新进度条
                    base.Level1Index = j;
                    Application.DoEvents();
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }