失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Wix 安装部署教程(十三) -- 多语言安装包

Wix 安装部署教程(十三) -- 多语言安装包

时间:2018-07-08 08:49:59

相关推荐

Wix 安装部署教程(十三) --  多语言安装包

原文:Wix 安装部署教程(十三) -- 多语言安装包

这几天摸索WIX的多语言安装包(这里是Wix的setup 工程,不是Bundle),终于走通了,感谢网友uni的指点。WIX的多语言安装包能够根据系统环境自动切换界面语言,你也可以通过命令指定语言。下面我说一说步骤。共4步。

1.设置WixLocalization文件。

Wxl文件就相当于应用程序的资源文件。让我们根据不同的语言来编写不同的文本内容。

例如我们新建一个WixUI_zh-cn.wxl ,来处理简体中文。

<WixLocalization Culture="zh-cn" Codepage="936" xmlns="/wix//localization"><String Id="WixUIBack" Overridable="yes">上一步(&amp;B)</String><String Id="WixUINext" Overridable="yes">下一步(&amp;N)</String><String Id="WixUICancel" Overridable="yes">取消</String><String Id="WixUIFinish" Overridable="yes">完成(&amp;F)</String><String Id="WixUIRetry" Overridable="yes">重试(&amp;R)</String><String Id="WixUIIgnore" Overridable="yes">忽略(&amp;I)</String><String Id="WixUIYes" Overridable="yes">是(&amp;Y)</String><String Id="WixUINo" Overridable="yes">否(&amp;N)</String><String Id="WixUIOK" Overridable="yes">确定</String><String Id="WixUIPrint" Overridable="yes">打印(&amp;P)</String>

......

这些主要是用于UI界面上的元素,我们看Wix的窗口源码:下面的!(loc.WixUINext)和!(loc.WixUICancel) 和上面的Id为WixUINext和Id为WixUICancel的String 一一 对应。而属性Overridable,表示可以重写。因为默认是英文,可以重写的意思就是最新的String会覆盖之前的同名Id。

<Dialog Id="WelcomeDlg" Width="370" Title="!(loc.WelcomeDlg_Title)"><Control Id="Next" Type="PushButton" X="236" Y="243" Width="56"Height="17" Default="yes" Text="!(loc.WixUINext)" /><Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)"><Publish Event="SpawnDialog" Value="CancelDlg">1</Publish></Control>

如果要添加自定义的内容,添加String就行,另外我们可以看见,这个wxl文件有Culture="zh-cn" Codepage="936"两个属性,这是和Product的Language属性和Codepage属性对应,而Product的Codepage属性不能使用!loc.Id 的方式,但Wix会根据wxl文件进行重载。Package的Languages属性和SummaryCodepage 也是一样对应。

<String Id="Lang">2052</String><String Id="Code">936</String>

下面是三种语言的Language和Codepage。

语言语言-国家 Language Codepage

Englishen-us10331252

Simplified Chinese zh-cn 2052936

Traditional Chinese zh-tw 1028950

同理我们分别添加英文和繁体的wxl。更多的源文件在源码的下面的目录可以找到:

WIX\wix-3.8\src\ext\UIExtension\wixlib

最终我们得到:

这个时候不同语言设置的工作我们已经完成。下面设置下属性--Build--Cultures to build,生成安装包。

但是我们看到这三个安装文件,这不是我们想要的。毕竟三个文件都是一样的大。 我们继续处理。将多个msi文件融合成一个。

2.生成mst文件

这里要用到transform files(变形文件?恩,就这么翻译吧) 一个变形文件(.mst)包含了两个msi文件的比较内容,在做Path的时候,也会用到它。既然是比较内容,就需要有一个文件作为基础,这次我们选英文。另外我们会用到一个wix工具:Torch.exe,它在Wix的安装目录bin下面可以找到。它的命令语法如下:

torch.exe [-?] [options] targetInput updatedInput -out outputFile [@responseFile]

示例:

torch.exe -t language "en-us\MyInstaller.msi""zh-cn\MyInstaller.msi" -out "transforms\zn-cn.mst"

别忘了用管理员身份执行,还要带上正确的路径。生成的mst文件再wix的bin目录,也就是和torch.exe的同级目录。如下图所示,操作正确会出现 Windows Installer XML Toolset。。。的字样。

同理我们生成zh-tw.mst和zh-cn.mst,要生成en-us.mst,就把中文和英文的位置换一下。 生成文件如下。

3.合并mst文件。

步骤2相当于我们拿到三个界面的资源文件,现在就是需要把他们合并成一个文件,也就是把mst嵌入到msi文件中。WIX本身没有提供工具做这个事情,但Windows 的SDK 有几个VBScript文件能执行数据库(MSI本身就是一种数据库文件)相关的任务。你需要从MSDN下载安装SDK,这些脚本包含在Win32的示例中。你也可以从下面的链接了解更多内容。

/en-us/library/aa372865(VS.85).aspx

而我们感兴趣的脚本是WixSubStg.vbs. win7系统,它的目录应该是在C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\msi\scripts. 中。找到之后我们复制到安装工程的Debug目录下。

WixSubStg.vbs:

' Windows Installer utility to add a transform or nested database as a substorage' For use with Windows Scripting Host, CScript.exe or WScript.exe' Copyright (c) Microsoft Corporation. All rights reserved.' Demonstrates the use of the database _Storages table'Option ExplicitConst msiOpenDatabaseModeReadOnly= 0Const msiOpenDatabaseModeTransact= 1Const msiOpenDatabaseModeCreate = 3Const msiViewModifyInsert = 1Const msiViewModifyUpdate = 2Const msiViewModifyAssign = 3Const msiViewModifyReplace = 4Const msiViewModifyDelete = 6Const ForAppending = 8Const ForReading = 1Const ForWriting = 2Const TristateTrue = -1' Check arg count, and display help if argument not present or contains ?Dim argCount:argCount = Wscript.Arguments.CountIf argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0If (argCount = 0) ThenWscript.Echo "Windows Installer database substorage managment utility" &_vbNewLine & " 1st argument is the path to MSI database (installer package)" &_vbNewLine & " 2nd argument is the path to a transform or database to import" &_vbNewLine & " If the 2nd argument is missing, substorages will be listed" &_vbNewLine & " 3rd argument is optional, the name used for the substorage" &_vbNewLine & " If the 3rd arugment is missing, the file name is used" &_vbNewLine & " To remove a substorage, use /D or -D as the 2nd argument" &_vbNewLine & " followed by the name of the substorage to remove" &_vbNewLine &_vbNewLine & "Copyright (C) Microsoft Corporation. All rights reserved."Wscript.Quit 1End If' Connect to Windows Installer objectOn Error Resume NextDim installer : Set installer = NothingSet installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError' Evaluate command-line arguments and set open and update modesDim databasePath:databasePath = Wscript.Arguments(0)Dim openMode : If argCount = 1 Then openMode = msiOpenDatabaseModeReadOnly Else openMode = msiOpenDatabaseModeTransactDim updateMode : If argCount > 1 Then updateMode = msiViewModifyAssign 'Either insert or replace existing rowDim importPath : If argCount > 1 Then importPath = Wscript.Arguments(1)Dim storageName : If argCount > 2 Then storageName = Wscript.Arguments(2)If storageName = Empty And importPath <> Empty Then storageName = Right(importPath, Len(importPath) - InStrRev(importPath, "\",-1,vbTextCompare))If UCase(importPath) = "/D" Or UCase(importPath) = "-D" Then updateMode = msiViewModifyDelete : importPath = Empty 'substorage will be deleted if no input data' Open database and create a view on the _Storages tableDim sqlQuery : Select Case updateModeCase msiOpenDatabaseModeReadOnly: sqlQuery = "SELECT `Name` FROM _Storages"Case msiViewModifyAssign: sqlQuery = "SELECT `Name`,`Data` FROM _Storages"Case msiViewModifyDelete: sqlQuery = "SELECT `Name` FROM _Storages WHERE `Name` = ?"End SelectDim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckErrorDim view: Set view = database.OpenView(sqlQuery)Dim recordIf openMode = msiOpenDatabaseModeReadOnly Then 'If listing storages, simply fetch all recordsDim message, nameview.Execute : CheckErrorDoSet record = view.FetchIf record Is Nothing Then Exit Doname = record.StringData(1)If message = Empty Then message = name Else message = message & vbNewLine & nameLoopWscript.Echo messageElse 'If adding a storage, insert a row, else if removing a storage, delete the rowSet record = installer.CreateRecord(2)record.StringData(1) = storageNameview.Execute record : CheckErrorIf importPath <> Empty Then 'Insert storage - copy data into streamrecord.SetStream 2, importPath : CheckErrorElse 'Delete storage, fetch first to provide better error message if missingSet record = view.FetchIf record Is Nothing Then Wscript.Echo "Storage not present:", storageName : Wscript.Quit 2End Ifview.Modify updateMode, record : mit : CheckErrorSet view = NothingSet database = NothingCheckErrorEnd IfSub CheckErrorDim message, errRecIf Err = 0 Then Exit Submessage = Err.Source & " " & Hex(Err) & ": " & Err.DescriptionIf Not installer Is Nothing ThenSet errRec = installer.LastErrorRecordIf Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatTextEnd IfWscript.Echo messageWscript.Quit 2End Sub

View Code

打开cmd 执行下面的命令:

WiSubStg.vbs "en-us\DIAViewSetup.msi" "transforms\zh-cn.mst" 2052

WiSubStg.vbs "en-us\DIAViewSetup.msi" "transforms\zh-tw.mst" 1028

等于是在英文的安装包里面嵌入了简体和繁体的语言变形。 所以我们不必嵌入英文的变形。 完成了这个步骤还不够。我们还需要改变安装包Packages的Languages属性,告诉它支持的语言类型有哪些。而这需要用到另外一个脚本文件

WiLangId.vbs:也需要放于Debug目录下面。

' Windows Installer utility to report the language and codepage for a package' For use with Windows Scripting Host, CScript.exe or WScript.exe' Copyright (c) Microsoft Corporation. All rights reserved.' Demonstrates the access of language and codepage values 'Option ExplicitConst msiOpenDatabaseModeReadOnly= 0Const msiOpenDatabaseModeTransact= 1Const ForReading = 1Const ForWriting = 2Const TristateFalse = 0Const msiViewModifyInsert = 1Const msiViewModifyUpdate = 2Const msiViewModifyAssign = 3Const msiViewModifyReplace = 4Const msiViewModifyDelete = 6Dim argCount:argCount = Wscript.Arguments.CountIf argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0If (argCount = 0) Thenmessage = "Windows Installer utility to manage language and codepage values for a package." &_vbNewLine & "The package language is a summary information property that designates the" &_vbNewLine & " primary language and any language transforms that are available, comma delim." &_vbNewLine & "The ProductLanguage in the database Property table is the language that is" &_vbNewLine & " registered for the product and determines the language used to load resources." &_vbNewLine & "The codepage is the ANSI codepage of the database strings, 0 if all ASCII data," &_vbNewLine & " and must represent the text data to avoid loss when persisting the database." &_vbNewLine & "The 1st argument is the path to MSI database (installer package)" &_vbNewLine & "To update a value, the 2nd argument contains the keyword and the 3rd the value:" &_vbNewLine & " Package {base LangId optionally followed by list of language transforms}" &_vbNewLine & " Product {LangId of the product (could be updated by language transforms)}" &_vbNewLine & " Codepage {ANSI codepage of text data (use with caution when text exists!)}" &_vbNewLine &_vbNewLine & "Copyright (C) Microsoft Corporation. All rights reserved."Wscript.Echo messageWscript.Quit 1End If' Connect to Windows Installer objectOn Error Resume NextDim installer : Set installer = NothingSet installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError' Open databaseDim databasePath:databasePath = Wscript.Arguments(0)Dim openMode : If argCount >= 3 Then openMode = msiOpenDatabaseModeTransact Else openMode = msiOpenDatabaseModeReadOnlyDim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError' Update value if suppliedIf argCount >= 3 ThenDim value:value = Wscript.Arguments(2)Select Case UCase(Wscript.Arguments(1))Case "PACKAGE" : SetPackageLanguage database, valueCase "PRODUCT" : SetProductLanguage database, valueCase "CODEPAGE" : SetDatabaseCodepage database, valueCase Else : Fail "Invalid value keyword"End SelectCheckErrorEnd If' Extract language info and compose report messageDim message:message = "Package language = " & PackageLanguage(database) &_", ProductLanguage = " & ProductLanguage(database) &_", Database codepage = " & DatabaseCodepage(database)mit : CheckError ' no effect if opened ReadOnlySet database = nothingWscript.Echo messageWscript.Quit 0' Get language list from summary informationFunction PackageLanguage(database)On Error Resume NextDim sumInfo : Set sumInfo = database.SummaryInformation(0) : CheckErrorDim template : template = sumInfo.Property(7) : CheckErrorDim iDelim:iDelim = InStr(1, template, ";", vbTextCompare)If iDelim = 0 Then template = "Not specified!"PackageLanguage = Right(template, Len(template) - iDelim)If Len(PackageLanguage) = 0 Then PackageLanguage = "0"End Function' Get ProductLanguge property from Property tableFunction ProductLanguage(database)On Error Resume NextDim view : Set view = database.OpenView("SELECT `Value` FROM `Property` WHERE `Property` = 'ProductLanguage'")view.Execute : CheckErrorDim record : Set record = view.Fetch : CheckErrorIf record Is Nothing Then ProductLanguage = "Not specified!" Else ProductLanguage = record.IntegerData(1)End Function' Get ANSI codepage of database text dataFunction DatabaseCodepage(database)On Error Resume NextDim WshShell : Set WshShell = Wscript.CreateObject("Wscript.Shell") : CheckErrorDim tempPath:tempPath = WshShell.ExpandEnvironmentStrings("%TEMP%") : CheckErrordatabase.Export "_ForceCodepage", tempPath, "codepage.idt" : CheckErrorDim fileSys : Set fileSys = CreateObject("Scripting.FileSystemObject") : CheckErrorDim file : Set file = fileSys.OpenTextFile(tempPath & "\codepage.idt", ForReading, False, TristateFalse) : CheckErrorfile.ReadLine ' skip column name recordfile.ReadLine ' skip column defn recordDatabaseCodepage = file.ReadLinefile.CloseDim iDelim:iDelim = InStr(1, DatabaseCodepage, vbTab, vbTextCompare)If iDelim = 0 Then Fail "Failure in codepage export file"DatabaseCodepage = Left(DatabaseCodepage, iDelim - 1)fileSys.DeleteFile(tempPath & "\codepage.idt")End Function' Set ProductLanguge property in Property tableSub SetProductLanguage(database, language)On Error Resume NextIf Not IsNumeric(language) Then Fail "ProductLanguage must be numeric"Dim view : Set view = database.OpenView("SELECT `Property`,`Value` FROM `Property`")view.Execute : CheckErrorDim record : Set record = installer.CreateRecord(2)record.StringData(1) = "ProductLanguage"record.StringData(2) = CStr(language)view.Modify msiViewModifyAssign, record : CheckErrorEnd Sub' Set ANSI codepage of database text dataSub SetDatabaseCodepage(database, codepage)On Error Resume NextIf Not IsNumeric(codepage) Then Fail "Codepage must be numeric"Dim WshShell : Set WshShell = Wscript.CreateObject("Wscript.Shell") : CheckErrorDim tempPath:tempPath = WshShell.ExpandEnvironmentStrings("%TEMP%") : CheckErrorDim fileSys : Set fileSys = CreateObject("Scripting.FileSystemObject") : CheckErrorDim file : Set file = fileSys.OpenTextFile(tempPath & "\codepage.idt", ForWriting, True, TristateFalse) : CheckErrorfile.WriteLine ' dummy column name recordfile.WriteLine ' dummy column defn recordfile.WriteLine codepage & vbTab & "_ForceCodepage"file.Close : CheckErrordatabase.Import tempPath, "codepage.idt" : CheckErrorfileSys.DeleteFile(tempPath & "\codepage.idt")End Sub' Set language list in summary informationSub SetPackageLanguage(database, language)On Error Resume NextDim sumInfo : Set sumInfo = database.SummaryInformation(1) : CheckErrorDim template : template = sumInfo.Property(7) : CheckErrorDim iDelim:iDelim = InStr(1, template, ";", vbTextCompare)Dim platform : If iDelim = 0 Then platform = ";" Else platform = Left(template, iDelim)sumInfo.Property(7) = platform & languagesumInfo.Persist : CheckErrorEnd SubSub CheckErrorDim message, errRecIf Err = 0 Then Exit Submessage = Err.Source & " " & Hex(Err) & ": " & Err.DescriptionIf Not installer Is Nothing ThenSet errRec = installer.LastErrorRecordIf Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatTextEnd IfFail messageEnd SubSub Fail(message)Wscript.Echo messageWscript.Quit 2End Sub

View Code

语法:

WiLangId.vbs "en-us\TestInstaller.msi" Package 1033,1028,2052

执行成功会出现下面的提示框:

这个时候,我们再点击英文的安装包。出现的是中文界面了。因为它已经根据我的系统环境自动做了选择。测试了繁体系统也是ok的。

4.用命令选择语言。

但是,这都还不是完整的多语言。有时候用户需要选择,比如他想再繁体系统中使用简体安装界面,而且我们的安装界面的多语言需要和我们应用程序的多语言同步起来。基于这个需求,我们还是需要在msi文件上包一个壳,先让用户去选择一门语言。那么我们怎么用命令来执行呢?

msiexec有命令:

msiexec /i en-us\DIAViewSetup.msi TRANSFORMS=transforms\zh-tw.mst

指定在简体系统中使用繁体界面:

这样在我们的C++外壳中就可以根据用户的选择也触发不同的界面。如果用户自己手动去安装,也起码可以根据他当前的系统自动切换语言。

小结:本文没有提供完整的安装包多语言解决方法。仅仅为大家提供了一种我走通了的方法,另外还有许可证书的部分,不同的语言,不同的许可证书,但路径又是不能通过loc的方式来处理的。需要自定义窗口来处理,后面有新的发现我会继续更新。希望对苦逼的WIX学习者带来帮助。

如果觉得《Wix 安装部署教程(十三) -- 多语言安装包》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。