⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 可批量将资源中词汇进行语言转换
💻 CS
📖 第 1 页 / 共 3 页
字号:
        public void WriteFile(string fileName,string source,string senc) //#region 
        {
            string sencode="utf-8";
            try
            {
                if (senc=="utf-8") //#region 
                {
                    sencode="utf-8";
                } //#endregion 
                else //#region 
                {
                    sencode="gb2312";
                } //#endregion  
                byte[] output = Encoding.GetEncoding(sencode).GetBytes(source);
                System.IO.FileStream fs = new FileStream(fileName  ,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
                fs.Write(output,0,output.Length);
                fs.Close();

            }
            catch{}
        } //#endregion 

       
		public void WriteFile(string fileName,string source) //#region 
        {
            try
            {
                byte[] output = Encoding.GetEncoding("utf-8").GetBytes(source);
                System.IO.FileStream fs = new FileStream(fileName  ,FileMode.Create,FileAccess.Write,FileShare.ReadWrite);
                fs.Write(output,0,output.Length);
                fs.Close();

            }
            catch{}

        } //#endregion 

        private void btnWrite_Click(object sender, System.EventArgs e) //#region 
        {
            // changeResx to cs
            // 修改记录 2007-07-18  CMMDB00257395 开始
            //changeResx(this.txtResxSourcePath.Text,this.txtResxDestPath.Text);
            // MessageBox.Show("成功");
            //changeCS(this.txtResxSourcePath.Text,this.txtResxDestPath.Text);
            //txtResxSourcePath end with "\"
			dtGlobal = LoadExcel(this.txtResxSourcePath.Text + "output.xls");
            //changeCS(this.txtResxSourcePath.Text,this.txtResxDestPath.Text,"*","*.cs");
            changeCS(this.txtResxSourcePath.Text,this.txtResxDestPath.Text,"*",txtExt.Text);
            
            MessageBox.Show("成功");
            // 修改记录 2007-07-18  CMMDB00257395 结束
        } //#endregion 
        /// <summary>
        /// 获取数据:从Excel 文件读入资源文件
        /// </summary>
        private DataTable LoadExcel(string fileName) //#region 
        {
            string strConnection= @"Provider=Microsoft.JET.OLEDB.4.0;Extended Properties=Excel 8.0;data source=" + fileName;
            string strSQL="";
            strSQL= "select * from [output.xls$]";
            OleDbConnection conn=new OleDbConnection(strConnection);
            OleDbDataAdapter da=new OleDbDataAdapter(strSQL,conn);
            DataSet ds=new DataSet();
			try
			{
				da.Fill(ds);
			}
			catch (Exception e)
			{
				this.labelMSG.Text= e.Message;
				
				throw new Exception(e.Message);
			}
            //绑定显示
            return ds.Tables[0];
        } //#endregion 
        /// <summary>
        /// 获取数据:从Excel 文件读入资源文件 find first match's english str(中文名 AND 文件名 same)
        /// </summary>
        private string FindEnglishFromExcel(string chsName,DataTable dt,string fileName) //#region 
        {
            string strReturn = chsName;
            for(int n=0;n<dt.Rows.Count ;n++) //#region 
            {
                DataRow dr=dt.Rows[n];
                string strName = dr["中文名"].ToString().Trim();
                string strFileName = dr["文件名"].ToString().Trim();

                bool isFnameMatch=(strFileName.Substring(strFileName.LastIndexOf("\\")+1)==fileName.Substring(fileName.LastIndexOf("\\")+1))?true:false; 

                if ( (strName == chsName) && 
                    ( isFnameMatch ) )
                {
                    strReturn =  dr["英文名"].ToString().Trim();
                    break;
                }

            } //#endregion 
            return strReturn;
        } //#endregion 


        // return all english strs with "\r\n" seperated. english str defined in excel
        private string LoadFileToResxStr(string path,DataTable dt) //#region 
        {
            string resultStr = "";
            try 
            {

                //using (StringReader sr = new StringReader(ReadFile(path))) 
                using (StringReader sr = new StringReader(ReadFile(path,"gb2312"))) 
                {  //#region 
                    String line;
                    int lineNum = 0;
                    bool bDisable = false;
                    string lineStr = "";
                    string toLine = "";
                    while ((line = sr.ReadLine()) != null) 
                    { //#region 
                        lineNum ++;
                        lineStr = line;
                        toLine = line;
                        if (lineStr.IndexOf("//") != -1)
                        {
                            lineStr = line.Substring(0,line.IndexOf("//"));

                        }
                        if (bDisable)
                        {
                            if (lineStr.IndexOf("*/") != -1)
                            {
                                lineStr = lineStr.Substring(lineStr.IndexOf("*/") + 2);
                                bDisable = false;

                            }
                            else
                            {
                                lineStr = "";

                            }

                        }
                        else
                        {
                            if (lineStr.IndexOf("/*") != -1)
                            {
                                lineStr = lineStr.Substring(0,lineStr.IndexOf("/*"));
                                bDisable = true;

                            }

                        }
                        if (lineStr.IndexOf("--") != -1)
                        {
                            lineStr = line.Substring(0,line.IndexOf("--"));

                        }
                        if (lineStr.IndexOf("#region") != -1)
                        {
                            lineStr = line.Substring(0,line.IndexOf("#region"));

                        }
                        if (lineStr.IndexOf("#endregion") != -1)
                        {
                            lineStr = line.Substring(0,line.IndexOf("#endregion"));

                        }

                        // lineStr's each chinese str replace to english via excel
                        if (lineStr != "")  //#region 
                        {
                            ArrayList myAL = FindChineseStr(lineStr);
                            if (myAL.Count > 0)
                            {
                                for (int i = 0; i< myAL.Count; i++) //#region 
                                {
                                    // special line "宋体" excluded
                                    if ( !myAL[i].Equals("宋体"))
                                    {
                                        toLine = toLine.Replace(myAL[i].ToString(),FindEnglishFromExcel(myAL[i].ToString(),dt,path));

                                    }

                                } //#endregion 
                               
                            }
                        }  //#endregion 
                        resultStr = resultStr + toLine + "\r\n";

                    } //#endregion 
                } //#endregion end using
                
            } 
            catch (Exception ee) 
            {  //#region 
                string str =ee.Message;
            } // #endregion 
            return resultStr;
        } //#endregion 


        // excel中已记录原始文件名 返回"find"或"";
        private string FindSourceFilenameInExcel(DataTable dt,string fileName) //#region 
        {
            string strReturn = "";
            for(int n=0;n<dt.Rows.Count ;n++) //#region 
            {
                DataRow dr=dt.Rows[n];
                string strFileName = dr["文件名"].ToString().Trim();

                //bool isFnameMatch=(strFileName.Substring(strFileName.LastIndexOf("\\")+1)==fileName.Substring(fileName.LastIndexOf("\\")+1))?true:false; 

				bool isFnameMatch=(strFileName.ToLower()==fileName.ToLower())?true:false;
                if ( (1==1) && 
                    ( isFnameMatch ) )
                {
                    strReturn = "find";
                    break;
                }

            } //#endregion 
            return strReturn;
        } //#endregion 


        // output.xls should be in sourcePath
        private void changeCS(string sourcePath,string destPath,string dirStr,string fileStr) //#region 
        {
            try 
            {
                DirectoryInfo di = new DirectoryInfo(sourcePath);
                FileSystemInfo[] dirs = di.GetDirectories(dirStr);
                string [] arr =  fileStr.Split(new Char [] {'|'});
                //DataTable dt = LoadExcel(sourcePath + "output.xls");
				DataTable dt = dtGlobal;

				string sExt =fileStr.Substring(1,fileStr.Length-1);

                // subfix now ".cs"
                for (int i = 0;i< arr.Length;i++) //#region 
                {
                    FileInfo[] fi = di.GetFiles(arr[i]);

                    foreach (FileInfo fiTemp in fi) //#region 
                    {
                        
                        string strContent="";
                        bool isfnameRecord= (FindSourceFilenameInExcel(dt,fiTemp.FullName)=="find")?true:false;
                        // only have 1st dot BUG here!
						if (isfnameRecord) 
						{ //#region
							strContent = LoadFileToResxStr(sourcePath + fiTemp.Name,dt);
							//string sTargetFile=destPath + fiTemp.Name.Substring(0,fiTemp.Name.IndexOf(".") ) + ".cs";
							string sTargetFile=destPath + fiTemp.Name.Substring(0,fiTemp.Name.IndexOf(".") ) + sExt;  // not *.cs but .cs
							if(Directory.Exists(destPath)==false)
							{
								Directory.CreateDirectory(destPath);
							}
                            // output.xls sheet里面的内容源必须是使用gb2312存放的
                            if (checkBox1.Checked==true)  //#region 
                            { 
                                WriteFile(sTargetFile,strContent,"gb2312");
                            }
                            else
                            {
                                WriteFile(sTargetFile,strContent,"utf-8");
                            } //#endregion 
						} //#endregion 
                    } //#endregion 
                    //是否空行
                    

                    foreach (DirectoryInfo diNext in dirs)  //#region 
                    {
                      //string sSourceDir=diNext.Substring(strFileName.LastIndexOf("\\")+1);
                      //string sParentDir=diNext.FullName.Substring(diNext.FullName.LastIndexOf("\\")+1);
                      string sdesttDir=diNext.FullName.Replace(sourcePath,destPath);
                      changeCS(diNext.FullName + "\\",sdesttDir+"\\",dirStr,fileStr);
                    } //#endregion 

                } //#endregion    

            }  
            catch (Exception ee) 
            {
                string str =ee.Message;
            }
        } //#endregion 

        private void changeResx(string sourcePath,string destPath) //#region 
        {
            try 
            {
                DirectoryInfo di = new DirectoryInfo(sourcePath);
                FileInfo[] fi = di.GetFiles("*.resx");
                DataTable dt = LoadExcel(sourcePath + "output.xls");

                foreach (FileInfo fiTemp in fi)
                {
                    string strContent = LoadFileToResxStr(sourcePath + fiTemp.Name,dt);
                    WriteFile(destPath + fiTemp.Name.Substring(0,fiTemp.Name.IndexOf(".") ) + "_en-US.resx",strContent);

                    
                }

            } 
            catch (Exception ee) 
            {
                string str =ee.Message;
            }
        } //#endregion 


	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -