代码改变世界

Wix 安装部署教程(十六) -- 自动生成多语言文件

2015-11-02 19:39  stoneniqiu  阅读(2267)  评论(0编辑  收藏  举报

     因为持续集成需要,所有项目编译完之后生成一个多语言的安装包。之前生成mst文件都是手动操作,而且mst文件必须每次重新和新的安装包“关联”,否则中文的安装包去调用英文的资源的时候就会报类似于“类型转换失败”的错误。基于这两点,有必要程序化这个流程,不然每次打包都得找我了。以下是程序的记录。比较简单。

     其实就是用程序调用cmd,再次之前,请记得将wix的bin目录加入到系统变量中。否则命令不会被识别;然后将程序执行目录指向目标目录。 

 static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                return;
            }
            Console.WriteLine(args[0]);

            var path = args[0];
          //  var path =MsiPath;
            
            Console.WriteLine("正在执行:" + DateTime.Now);
            var p = new Process
            {
                StartInfo =
                {
                    FileName = "cmd.exe ",
                    UseShellExecute = false,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = false,
                    WorkingDirectory = path
                }
            };
            try
            {
                p.Start();

                if (!Init(p, path))
                {
                    Console.WriteLine("初始化失败");
                    p.Close();
                    return;
                }
                //合并语言
                ExcComand(p, CommandLines.LanZh_TW);
                ExcComand(p, CommandLines.LanZh_CN);
                ExcComand(p, CommandLines.LanEN_US);
                
                //关联语言
                ExcComand(p, CommandLines.CombanLanZh_cn);
                //测试安装包
               // ExcComand(p, CommandLines.Testzh_Tw);

                //p.WaitForExit();
                p.Close();
                Console.WriteLine("Complete" + DateTime.Now);
            }
            catch (Exception e1)
            {
                Console.WriteLine("error" + e1.Message);
            }

        }

  先初始化,确保安装文件、vbs文件和mst文件都存在。

  private static string[] lans = { "zh-cn", "en-us", "zh-tw" };
        private static bool Init(Process p, string path)
        {
            //多语言安装包是否存在 
            foreach (var lan in lans)
            {
                var lanPath = Path.Combine(path, lan, SoftName);
                if (!File.Exists(lanPath))
                {
                    Console.WriteLine("{0}安装包不存在!",lan);
                    return false;
                }
            }
            //保证两个文件
            CopyFileIfNotExist(path,"WixSubStg.vbs");
            CopyFileIfNotExist(path, "WiLangId.vbs");

            //变形文件是否存在
            var twmst = Path.Combine(path, "transforms", "zh-tw.mst");
            if (!File.Exists(twmst))
            {
                GetTransforms(p);
            }

            //再检测一次
            return File.Exists(twmst);
        }

这两个vbs文件,在教程十三里面有提供。 最后在检查一次mst文件就是确保mst文件的存在。变形文件是我们多语言安装包的关键。

  /// <summary>
        /// 生成变形文件
        /// </summary>
        /// <param name="p"></param>
        private static void GetTransforms(Process p)
        {
            ExcComand(p, CommandLines.EnToZh);
            ExcComand(p, CommandLines.EnToTw);
            ExcComand(p, CommandLines.ZhToEn);
        }

        private static void ExcComand(Process p, string command)
        {
            p.StandardInput.WriteLine(command);
            Console.WriteLine("command:" + command);
            Thread.Sleep(1500);
        }
View Code

而commandLines 这个类里面就包含了这次所会用到的命令。

public  class CommandLines
  {
      //对应繁体
      public static string LanZh_TW = @"WixSubStg.vbs zh-cn\DIAViewSetup.msi  transforms\zh-tw.mst 1028";
      //对应中文
      public static string LanZh_CN = @"WixSubStg.vbs zh-cn\DIAViewSetup.msi transforms\zh-cn.mst 2052";
      //对应英文
      public static string LanEN_US = @"WixSubStg.vbs zh-cn\DIAViewSetup.msi transforms\en-us.mst 1033";

      //合成语言安装包 ,默认是中文
      public static string CombanLanZh_cn = @"WiLangId.vbs zh-cn\DIAViewSetup.msi Package 1028,2052,1033";
      //测试繁体安装包
      public static string Testzh_Tw = @"msiexec /i  zh-cn\DIAViewSetup.msi TRANSFORMS=transforms\zh-tw.mst";
      //测试中文安装包
      public static string Testzh_Cn = @"msiexec /i  zh-cn\DIAViewSetup.msi TRANSFORMS=transforms\zh-cn.mst";
      //测试英文安装包
      public static string Testen_US = @"msiexec /i  zh-cn\DIAViewSetup.msi TRANSFORMS=transforms\en-us.mst";

      //生成英文资源
      public static string EnToZh = @"torch.exe -t language en-us\DIAViewSetup.msi zh-cn\DIAViewSetup.msi -out transforms\zh-cn.mst";
      //生成中文资源
      public static string EnToTw = @"torch.exe -t language en-us\DIAViewSetup.msi zh-tw\DIAViewSetup.msi -out transforms\zh-tw.mst";
      //生成繁体资源
      public static string ZhToEn = @"torch.exe -t language zh-cn\DIAViewSetup.msi en-us\DIAViewSetup.msi -out transforms\en-us.mst";
  }
View Code

最后的执行结果如下。 这里是以zh-cn为基础。合成之后的安装包就可以根据系统环境自动切换语言,如果不是关联的语言之一就显示默认语言。

 当然,主要的目的是可以让他可以根据用户的选择来显示不同的语言界面。