C#将CSV文件转为资源文件--语言切换

1.对CSV进行操作的函数:将CSV数据转为datatable

 class CsvLib
    {    /// <summary>
        /// 将Csv读入DataTable
        /// </summary>
        /// <param name="filePath">csv文件路径</param>
        /// <param name="n">表示第n行是字段title,第n+1行是记录开始</param>
        public static DataTable Csv2dt(string filePath, int n)
        {
            DataTable dt = new DataTable();
            String csvSplitBy = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
            StreamReader reader = new StreamReader(filePath, System.Text.Encoding.Default, false);
            //StreamReader reader = new StreamReader(filePath, System.Text.Encoding.Unicode, false);如果出现乱码多换几种编码方式试一下
            int i = 0, m = 0;
            reader.Peek();
            while (reader.Peek() > 0)
            {
                m = m + 1;
                string str = reader.ReadLine();
                if (m >= n + 1)
                {
                    if (m == n + 1) //如果是字段行,则自动加入字段。
                    {
                        MatchCollection mcs = Regex.Matches(str, csvSplitBy);
                        foreach (Match mc in mcs)
                        {
                            dt.Columns.Add(mc.Value); //增加列标题
                        }
                    }
                    else
                    {
                        MatchCollection mcs = Regex.Matches(str, "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)");
                        i = 0;
                        System.Data.DataRow dr = dt.NewRow();
                        foreach (Match mc in mcs)
                        {
                            dr[i] = mc.Value;
                            i++;
                        }
                        dt.Rows.Add(dr);  //DataTable 增加一行     
                    }
                }
            }
            return dt;
        }
    }

2.对datatable操作,将对应字段与值插入资源文件,并保存

			DataTable csv_dt = new DataTable();
            string filePath = "E:...resource.csv";
            csv_dt = CsvLib.Csv2dt(filePath, 0);
            int rowCount = csv_dt.Rows.Count;
            string language = Properties.Settings.Default.DefaultLanguage;

            DataView dv = csv_dt.DefaultView;
            DataTable forms = dv.ToTable(true, new string[] { "form" });
            for (int f = 0; f < forms.Rows.Count; f++)
            {

                //取出重复数据
                var dis_data = (from a in csv_dt.AsEnumerable()
                                group a by a.Field<string>("form")
                           into g
                                where g.Count() > 1
                                select new
                                {
                                    value = g.Key
                                }).ToList();

                if (dis_data.Count > 0)
                {
                    //拼接重复数据及重复数据出现的索引
                    Dictionary<string, List<int>> all_data = new Dictionary<string, List<int>>();
                    foreach (var dis in dis_data)
                    {
                        List<int> now_index = new List<int>();//出现重复的数据Index
                        for (int i = 0; i < csv_dt.Rows.Count; i++)
                        {
                            if (csv_dt.Rows[i]["form"].ToString() == dis.value)
                            {
                                now_index.Add(i + 2);//输出行数
                            }
                        }
                        all_data.Add(dis.value, now_index);
                    }
                    //遍历key
                    foreach (string key in all_data.Keys)
                    {
                        ResXResourceWriter rw_en = new ResXResourceWriter("E:...UI\\" + key + ".en.resx");
                        ResXResourceWriter rw_es = new ResXResourceWriter("E:...UI\\" + key + ".es.resx");
                        ResXResourceWriter rw_zh = new ResXResourceWriter("E:...UI\\" + key + ".zh.resx");
                        // 括号内是一个lambda表达式,num所代表的是aList集合中的一个元素
                        
                        
                        foreach (var  i in all_data[key])
                        {
                            string name = csv_dt.Rows[i-2]["text"].ToString();
                            string chinese = csv_dt.Rows[i-2]["Chinese"].ToString();
                            string english = csv_dt.Rows[i-2]["English"].ToString();
                            string spanish = csv_dt.Rows[i-2]["Spanish"].ToString();
                            rw_zh.AddResource(name, chinese);
                            rw_en.AddResource(name, english);
                            rw_es.AddResource(name, spanish);
                        }
                        rw_en.Generate();
                        rw_es.Generate();
                        rw_zh.Generate();
                        rw_en.Close();
                        rw_es.Close();
                        rw_zh.Close();
                    }
                }


3.读取资源文件:
自定义资源文件:public static ResourceManager res = new ResourceManager(typeof(nonUIresx));
组件资源文件:System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(formType);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值