/// <summary> /// 返回语言包数据 /// </summary> /// <param name="path"></param> /// <returns></returns> public Dictionary <string, string> GetLanPo(string path) { string PoFileCon = FileAction.ReadToStr(path); Dictionary <string, string> dic = new Dictionary <string, string>(); string reg = "msgid \"(?<msgid>.*)\"\nmsgstr \"(?<msgstr>.*)\""; string[] strArr = DataCheck.GetRegStrArr(PoFileCon, reg); foreach (var str in strArr) { MatchCollection res = Regex.Matches(str, reg, RegexOptions.IgnoreCase); foreach (Match item in res) { dic.Add(item.Groups["msgid"].Value, item.Groups["msgstr"].Value); } } return(dic); }
/// <summary> /// 翻译 /// </summary> /// <param name="path">语言包路径</param> /// <param name="url">翻译网址</param> /// <param name="Lan">翻译类型</param> public ActionResult Translate(string path, string url, string Lan) { string content = FileAction.ReadToStr(path); Dictionary <string, string> dic = new Dictionary <string, string>(); string reg = "msgid \"(?<msgid>.*)\"\r\nmsgstr \"(?<msgstr>.*)\""; string[] strArr = DataCheck.GetRegStrArr(content, reg); foreach (var str in strArr) { MatchCollection res = Regex.Matches(str, reg, RegexOptions.IgnoreCase); foreach (Match item in res) { dic.Add(item.Groups["msgid"].Value, item.Groups["msgstr"].Value); } } SaveLanJs(dic); // 发送翻译请求 // Dictionary<string, string> dicRes= BaiduTranslate(dic, Lan, ""); return(Content(ResObj.LayerMsg("转换成功!", Icon.Success, "Index"))); }
public void RepFile() { try { List <string> list = FileAction.ReadToArr(file.FullName); string p = file.FullName.Replace(path, outpath); // 匹配注释正则 string regNotes = "([/]{2,}|[*]+).*"; // 匹配中文正则 string regChinese = @"([\u4e00-\u9fa5]{1,}[\s,,‘“;(()):、:.&\\-a-zA-Z0-9\u4e00-\u9fa5]{0,}[。”’!0-9\u4e00-\u9fa5]{1,})|([\u4e00-\u9fa5]{1})"; int index = 0; foreach (var item in list) { index++; // 去掉注释 string str = DataCheck.RepStr(item.Trim(), regNotes, ""); //是否包含中文 if (!DataCheck.CheckReg(str, regChinese)) { FileAction.AppendStr(p, item + "\n"); } else { // 取出中文 string[] strArr = DataCheck.GetRegStrArr(str, regChinese); string temp = str; string get = ""; // 在语言包中寻找匹配 foreach (var chinese in strArr) { // 如果没有包含汉字,查找下一个 if (!DataCheck.CheckReg(chinese, "[\u4e00-\u9fa5]+")) { continue; } // 若语言包中存在对应中文,直接替换 if (dic.ContainsKey(chinese)) { get = dic[chinese]; temp = temp.Replace(chinese, DataCheck.RepLanguage(dic[chinese], false)); errInfo.Add($"{chinese}\t{dic[chinese]}\t{file.FullName}"); } // 否则,去寻找最类似的中文 else { // 获取极限长度 int min = chinese.Length - 2; int max = chinese.Length + 2; // 判断是否替换 bool bl = false; // 循环字典 foreach (var key in dic.Keys) { // 超出极限长度,则跳出 if (max < key.Length || key.Length < min) { continue; } // 若符合极限长度,且包含当前文字 if (key.Contains(chinese)) { // 进行替换 temp = temp.Replace(chinese, DataCheck.RepLanguage(dic[key], false)); errInfo.Insert(0, $"^{chinese}:{index}行\t{dic[key]}\t{file.FullName}"); bl = true; } } if (!bl) { errInfo.Insert(0, $"^^{chinese}:{index}行\t{file.FullName}"); } } } // 将当前行写入文件 FileAction.AppendStr(p, item.Replace(str, temp) + "\n"); } } } catch (Exception e) { errInfo.Add("错误:" + file.FullName + "\t" + e.Message); } }
private void Crawler_OnCompleted(object sender, OnCompletedEventArgs e) { // e.PageSource = DataCheck.RepTrim(e.PageSource); string[] str = DataCheck.GetRegStrArr(e.PageSource, e.Regex); }
private void bt_Start_Click(object sender, EventArgs e) { //CheckBox temp; //Crawler crawler = new Crawler(); //crawler.OnCompleted += Crawler_OnCompleted; //crawler.OnError += Crawler_OnError; //foreach (Control item in pl_UrlList.Controls) { // if (item is CheckBox) { // temp = item as CheckBox; // if (temp.Checked) { // SetLog("开始爬取:" + temp.Text, Color.Black); // crawler.Start(temp.Tag + "", "<a[^>]+?href=[\"']?http://3g.163.com/all/article/([^\"']+)[\"']?[^>]*>(.+?)</a>").Wait(); // } // temp.Enabled = true; // } //} //SetLog("结束!", Color.OrangeRed); #region 翻译 string ReadPath = @"F:\01.Porject\NanNing\050Coding\New_NaNing_SSC\WebFront\App_Data\Localization\en\orchard.module.po"; string SavePath = @"F:\orchard.module.po"; string content = FileAction.ReadToStr(ReadPath); string reg = "msgid \"(?<msgid>.*)\" \r\nmsgstr \"(?<msgstr>.*)\""; string[] strArr = DataCheck.GetRegStrArr(content, reg); Translate translate = new Translate("20171116000095832", "PuVyBlMqMOOjqfks4GJ7"); // 记录运行时间 Stopwatch watch = new Stopwatch(); int con = 0; foreach (var str in strArr) { SetLog("开始翻译!", Color.Black); watch.Start(); MatchCollection res = Regex.Matches(str, reg, RegexOptions.IgnoreCase); foreach (Match item in res) { // BaiduTransAPI transAPI = translate.BaiduTranslate(item.Groups["msgid"].Value, "zh", "en"); string context = "" + "msgid \"" + item.Groups["msgid"].Value + "\"\n" + "msgstr \"" + Utils.StrToUpper(item.Groups["msgstr"].Value, 2) + "\"\n\n"; FileAction.AppendStr(SavePath, context); } SetLog("翻译结束,用时:" + watch.ElapsedMilliseconds * 0.1 / 1000 + " s,第" + (++con) + "条", Color.Brown); watch.Stop(); } #endregion bt_Start.Enabled = true; }
private void Crawler_OnCompleted(object sender, OnCompletedEventArgs e) { string[] str = DataCheck.GetRegStrArr(e.PageSource, e.Regex); SetLog("抓取新闻数量:" + str.Length + "\t用时:" + e.Milliseconds, Color.Black); }