/// <summary> /// 追加文件 /// </summary> protected virtual void AddFile(string fileName, FilePosInfo fileInfo) { this.textFiles.Add(fileInfo); this.fileList.Items.Add(Util.GetShortFileName(fileInfo.File) + " " + fileInfo.TextStart.ToString("x") + "--" + fileInfo.TextEnd.ToString("x")); // 重新设置全路径的文件 fileInfo.File = fileName; }
/// <summary> /// 重新设置Entry位置信息 /// </summary> /// <param name="currentFileInfo">当前选择的文件</param> /// <param name="cnTxtLen">当前行中文文本字节长度</param> /// <param name="prevEntryPos">前一个Entry位置信息</param> /// <returns>当前Entry位置信息</returns> protected virtual int ResetEntryPosInfo(FilePosInfo currentFileInfo, int cnTxtLen, int prevEntryPos) { // 保存Entry的偏移 int nextEntryPos = prevEntryPos + cnTxtLen; currentFileInfo.TextEntrys.Add(nextEntryPos); return(nextEntryPos); }
/// <summary> /// 根据配置文件,读入需要汉化的文件 /// </summary> /// <param name="configFile"></param> /// <returns></returns> protected virtual List <FilePosInfo> LoadFiles(string configFile) { List <FilePosInfo> needCopyFiles = new List <FilePosInfo>(); if (File.Exists(configFile)) { string[] files = File.ReadAllLines(configFile); for (int i = 0; i < files.Length; i += 2) { FilePosInfo fileInfo = new FilePosInfo(files[i], files[i + 1].Split(' ')); needCopyFiles.Add(fileInfo); } } return(needCopyFiles); }
/// <summary> /// 文件变更 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void fileList_SelectedIndexChanged(object sender, EventArgs e) { if (this.fileList.SelectedIndex == -1 || this.fileList.SelectedIndex >= this.textFiles.Count) { return; } this.fileIndex = this.fileList.SelectedIndex; // 取得当前编辑文件 this.currentFileInfo = this.textFiles[this.fileList.SelectedIndex]; // 选择的文件变更 this.FileChanged(this.currentFileInfo); // 设置中文文件 this.cnFile = this.currentFileInfo.File.ToLower().Replace("jp", "cn"); // 解码文本 this.txtJp.Text = this.DecodeText(this.currentFileInfo, false); this.txtCn.Text = this.DecodeText(this.currentFileInfo, true); }
/// <summary> /// 导出文本 /// </summary> private void ExoprtText() { Microsoft.Office.Interop.Excel.Workbook xBook = null; Microsoft.Office.Interop.Excel.Worksheet xSheet = null; Microsoft.Office.Interop.Excel.Worksheet beforeSheet = null; // 设定保存的文件名 string fileName = this.baseFile; if (string.IsNullOrEmpty(fileName)) { fileName = @"TextExport.xlsx"; } // 先删除原来的文件 if (File.Exists(fileName)) { File.Delete(fileName); } // 显示进度条 this.ResetProcessBar(this.textFiles.Count); try { // 创建Application对象 this.xApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); // 追加一个WorkBook xBook = this.xApp.Workbooks.Add(Missing.Value); for (int j = 0; j < this.textFiles.Count; j++) { // 追加一个Sheet FilePosInfo filePosInfo = this.textFiles[j]; string jpText = string.Empty; string cnText = string.Empty; this.Invoke((MethodInvoker) delegate() { // 更新当前文本 this.fileList.SelectedIndex = j; // 取得日文、中文文本 jpText = this.txtJp.Text; cnText = this.txtCn.Text; }); // 设置当前Sheet名(如果有重复的就累加编号) string sheetName = Util.GetShortFileName(filePosInfo.File); int sameNameCount = 0; for (int i = 0; i < j; i++) { if (Util.GetShortFileName(this.textFiles[i].File).IndexOf(sheetName) >= 0) { sameNameCount++; } } if (beforeSheet == null) { xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); } else { xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets.Add(beforeSheet, Missing.Value, Missing.Value, Missing.Value); } xSheet.Name = sheetName + (sameNameCount > 0 ? "_" + sameNameCount.ToString() : string.Empty); beforeSheet = xSheet; // 将每行文本保存到Sheet中 string[] jpTexts = jpText.Split('\n'); string[] cnTexts = cnText.Split('\n'); for (int i = 0; i < jpTexts.Length; i++) { // 写入日文文本 Microsoft.Office.Interop.Excel.Range rngJp = xSheet.get_Range("A" + (i + 1), Missing.Value); rngJp.Value2 = jpTexts[i]; } for (int i = 0; i < cnTexts.Length; i++) { // 写入中文文本 Microsoft.Office.Interop.Excel.Range rngCn = xSheet.get_Range("G" + (i + 1), Missing.Value); rngCn.Value2 = cnTexts[i]; } // 更新进度条 this.ProcessBarStep(); } } finally { // 保存 xSheet.SaveAs( fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // 隐藏进度条 this.CloseProcessBar(); // 清空各种对象 xSheet = null; xBook = null; if (this.xApp != null) { this.xApp.Quit(); this.xApp = null; } // 显示保存完成信息 MessageBox.Show("导出完成!"); } }
/// <summary> /// 重新设置带Entry信息的翻译后的数据 /// </summary> /// <param name="currentFileInfo">当前选择的文件</param> /// <param name="byData">当前选择的文件的字节数据</param> /// <param name="cnBytes">翻译后的字节数据</param> /// <returns>带Entry信息的翻译后的数据</returns> protected virtual byte[] ResetCnDataWithEnrty(FilePosInfo currentFileInfo, byte[] byData, List <byte> cnBytes) { throw new Exception("需要重新设置带Entry信息的字节信息!"); }
/// <summary> /// 重新设置Entry位置信息 /// </summary> /// <param name="currentFileInfo">当前选择的文件</param> /// <param name="cnTxtLen">当前行中文文本字节长度</param> /// <param name="prevEntryPos">前一个Entry位置信息</param> /// <returns>当前Entry位置信息</returns> protected virtual int ResetLastEntryPosInfo(FilePosInfo currentFileInfo, int cnTxtLen, int prevEntryPos) { return(prevEntryPos + cnTxtLen); }
/// <summary> /// 保存存前的处理 /// </summary> /// <param name="byCnData">要保存的数据</param> /// <param name="fileInfo">当前文件信息</param> protected virtual void BeforeSave(byte[] byCnData, FilePosInfo fileInfo) { }
/// <summary> /// 开始解码文本 /// </summary> /// <param name="currentFileInfo">当前选择的文件</param> /// <param name="isCnTxt">是否是中文</param> /// <returns>解码的文本</returns> protected virtual string DecodeText(FilePosInfo currentFileInfo, bool isCnTxt) { return(string.Empty); }
/// <summary> /// 选择的文件变更 /// </summary> /// <param name="currentFileInfo">当前选择的文件</param> protected virtual void FileChanged(FilePosInfo currentFileInfo) { }