如何为多种语言自定义 NSIS 安装程序屏幕

How to customize the NSIS installer screens for multiple languages

我正在使用 NSIS 创建安装程序。在创建安装程序时,我需要根据我的要求自定义屏幕。我知道我们不应该修改 NSIS 包含的 .NSH 文件。

所以,我是这样做的,如下所示: 但在这里我需要为大约 9 种语言做这件事。有没有更好的方法来为每种语言分离单独的文件并在此处使用它? 以及如何将按钮和复选框作为自定义的一部分添加到许可协议屏幕?

;Include Modern UI

!include "MUI2.nsh"

RequestExecutionLevel admin

; Pages

;Page components
;Page directory
;Page instfiles

;UninstPage uninstConfirm
;UninstPage instfiles

;Customizing the Welcome Text

!define MUI_TEXT_WELCOME_INFO_TEXT "The Setup Wizard will install Test installation on$\r$\n your computer. Click Next to continue or Cancel to exit the$\r$\n Setup Wizard."
!define MUI_WELCOMEFINISHPAGE_BITMAP "C:\Code\Code\NULLSOFT\src\Bitmaps\dlgbmp-for-test.bmp"
!define MUI_PAGE_CUSTOMFUNCTION_SHOW MyWelcomeShowCallback

!insertmacro MUI_PAGE_WELCOME
  
;Customizing the License Page Text

!define MUI_TEXT_LICENSE_TITLE "End-User License Agreement"
!define MUI_TEXT_LICENSE_SUBTITLE "Please read the following license agreement carefully"
!define MUI_INNERTEXT_LICENSE_BOTTOM ""
!define MUI_INNERTEXT_LICENSE_TOP ""
!define MUI_PAGE_CUSTOMFUNCTION_LICENSESHOW MyLicenseShowCallback
 
  !insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\test.rtf"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_PAGE_FINISH

  !insertmacro MUI_UNPAGE_WELCOME
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_LICENSE "${NSISDIR}\Docs\Modern UI\test.rtf"
  !insertmacro MUI_UNPAGE_COMPONENTS
  !insertmacro MUI_UNPAGE_DIRECTORY
  !insertmacro MUI_UNPAGE_INSTFILES
  !insertmacro MUI_UNPAGE_FINISH
;--------------------------------

;Languages

  !insertmacro MUI_LANGUAGE "English" ; The first language is the default language
  !insertmacro MUI_LANGUAGE "PortugueseBR"
;--------------------------------
;Installer Functions

Function .onInit

FunctionEnd

;--------------------------------
; The stuff to install
; WELCOME PAGE CALLING FUNCTION
Function MyWelcomeShowCallback
SendMessage $mui.WelcomePage.Text ${WM_SETTEXT} 0 "STR:$(MUI_TEXT_WELCOME_INFO_TEXT)"
FunctionEnd


Function MyLicenseShowCallback
SendMessage $mui.LicensePage.Text ${WM_SETTEXT} 0 "STR:$(MUI_TEXT_LICENSE_TITLE)"
FunctionEnd

使用两个单独的 .nsh 文件(CustomChinese.nsh 和 CustomEnglish.nsh)更新了代码:

根据您的输入,我将每种语言的 !define 语句保存在单独的 .nsh 文件(CustomChinese.nsh 和 CustomEnglish.nsh)中,然后将这些文件包含在我的 .nsi 文件中。

但不允许包含两个 .nsh 文件(CustomChinese.nsh 和 CustomEnglish.nsh)和两个语言文件(!insertmacro MUI_LANGUAGE“英语”和 !insertmacro MUI_LANGUAGE“中文”)同时。所以,我每人只给了一个文件。但是如何提供多个文件以便根据区域设置获取适当的文件?

完整代码如下:

    Var IsOnLicensePage
    Var PrintBtn
    
    !include LogicLib.nsh
    !include "MUI2.nsh"
    
    Name "EMR 4.0"
    
    OutFile "LocaleDlls.exe"
    
    InstallDir $PROGRAMFILES\LocaleDlls
    
    InstallDirRegKey HKLM "Software\NSIS_LocaleDlls" "Install_Dir"
    
    ; Request application privileges for Windows Vista
    RequestExecutionLevel admin
    
    ;--------------------------------
    
    ; Pages
    
    ;Customizing the Welcome Text
      !define MUI_WELCOMEFINISHPAGE_BITMAP "C:\NULLSOFT\src\Bitmaps\dlgbmp-for-test.bmp"
      !insertmacro MUI_PAGE_WELCOME
    
      !define MUI_PAGE_CUSTOMFUNCTION_SHOW MyLicenseShowCallback
    
      !define MUI_LICENSEPAGE_CHECKBOX ""
      !insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\test\test.rtf"
    
      !insertmacro MUI_PAGE_DIRECTORY
      !insertmacro MUI_PAGE_INSTFILES
      !insertmacro MUI_PAGE_FINISH
    
      !insertmacro MUI_UNPAGE_WELCOME
      !insertmacro MUI_UNPAGE_CONFIRM
      !insertmacro MUI_UNPAGE_LICENSE "${NSISDIR}\Docs\test\test.rtf"
      !insertmacro MUI_UNPAGE_DIRECTORY
      !insertmacro MUI_UNPAGE_INSTFILES
      !insertmacro MUI_UNPAGE_FINISH
    ; Below are the Custom Language Files
      ;!include "CustomEnglish.nsh"
    
      !include "CustomChinese.nsh"
    ;--------------------------------
    
    ;Languages
    
      ;!insertmacro MUI_LANGUAGE "English" ; The first language is the default language
      !insertmacro MUI_LANGUAGE "Chinese"
    
    ;Installer Functions
    
    ;--------------------------------
    Function MyLicenseShowCallback
    
    GetDlgItem [=11=] $hwndparent 2 ; Find cancel button so we can copy its size and y position.
    System::Call *(i,i,i,i)p.r1
    System::Call 'USER32::GetWindowRect(pr0,pr1)'
    System::Call *(i.r2,i.r3,i.r4,i.r5)
    IntOp   -  ;height
    IntOp   -  ;width
    System::Call 'USER32::ScreenToClient(p$hwndparent,pr1)'
    System::Call *(i.r2,i.r3)
    System::Free 
    IntOp   / 5 ; Calculate x padding based on the width but you can put any value you want in 
    System::Call 'USER32::CreateWindowEx(i 0,t "Button",t "Print",i ${WS_CHILD}|${WS_VISIBLE}|${WS_TABSTOP},i r2,i r3,i r4,i r5,p $hwndparent,p 0x666,p 0,p 0)p.r0'
    StrCpy $PrintBtn [=11=]
    SendMessage $hwndparent ${WM_GETFONT} 0 0 
    SendMessage [=11=] ${WM_SETFONT}  1
    GetFunctionAddress [=11=] onmybtnclick
    ButtonEvent::AddEventHandler 0x666 [=11=]
    FunctionEnd
    
    Function onmybtnclick
    ExecShell "open" "$INSTDIR\test.rtf" "0"
    
    FunctionEnd
    
    ; The stuff to install
    Section "LocaleDlls (required)"
    
      SectionIn RO
    
      ; Set output path to the installation directory. Here is the path C:\Program Files\LocaleDlls
      SetOutPath $INSTDIR
    
      ; Give the File path
      System::Call 'KERNEL32::AddDllDirectory(w "$INSTDIR")' ; Tell Windows we trust all .DLLs in this directory
    
      ;MessageBox MB_OK "Return value = $Language"
    
      ${If} $Language == 1033
      MessageBox MB_OK "Current Locale is English... Loading English Files"
      
    
      ${ElseIf} $Language = 2052
      MessageBox MB_OK "Current Locale is Chinese... Loading Chinese Files"
      
      
      ${EndIf}
      
     ; Do the install
    
      ; Write the installation path into the registry
      WriteRegStr HKLM SOFTWARE\NSIS_DllTesting "Install_Dir" "$INSTDIR"
    
      ; Write the uninstall keys for Windows
      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "DisplayName" "NSIS LocaleDlls"
      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "UninstallString" '"$INSTDIR\uninstall.exe"'
      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "NoModify" 1
      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls" "NoRepair" 1
      WriteUninstaller "uninstall.exe"
    
    SectionEnd
    
   **Customtest.nsh**

!echo "NSIS Customtest"

!addincludedir "${NSISDIR}\Contrib\test 2"

;--------------------------------
;For Welcome Page


;For License Agreement Page

!define MUI_INNERTEXT_LICENSE_BOTTOM 
!define MUI_INNERTEXT_LICENSE_TOP 
!define MUI_INNERTEXT_LICENSE_BOTTOM_CHECKBOX 
!define MUI_LICENSEPAGE_CHECKBOX


!insertmacro MUI_LANGUAGE "Chinese"

**CustomEnglish.nsh**


!echo "NSIS CustomEnglish"

!addincludedir "${NSISDIR}\Contrib\Modern UI 2"

;--------------------------------
;For Welcome Page
!define MUI_TEXT_WELCOME_INFO_TITLE "Welcome to the $(^NameDA) Setup Wizard"
!define MUI_TEXT_WELCOME_INFO_TEXT "The Setup Wizard will install EMR on$\r$\nyour computer. Click Next to continue or Cancel to exit the$\r$\nSetup Wizard."

;For License Agreement Page
!define MUI_TEXT_LICENSE_TITLE "End-User License Agreement"
!define MUI_TEXT_LICENSE_SUBTITLE "Please read the following license agreement carefully"
!define MUI_INNERTEXT_LICENSE_BOTTOM ""
!define MUI_INNERTEXT_LICENSE_TOP ""
!define MUI_INNERTEXT_LICENSE_BOTTOM_CHECKBOX ""
!define MUI_LICENSEPAGE_CHECKBOX ""
!define MUI_LICENSEPAGE_CHECKBOX_TEXT "I &agree to terms in the License Agreement"
!insertmacro MUI_LANGUAGE "English"

下面是两个 .nsh 文件中带有 !ifdef 语句的更新代码片段:

即使对我来说,示例也能正常工作。但是如果我在“MyEnglish.nsh”和“MySwedish.nsh”中包含例如(MUI_CANCEL_MESSAGE“你确定要取消吗?”的 MUI 属性,它会显示编译器错误:!define : "MUI_CANCEL_MESSAGE" 已经定义!

代码如下:

    english.nsh
    
    !define MUI_CANCEL_MESSAGE "Are you sure you want to cancel EMR?"
    
    !insertmacro MUI_LANGUAGE "English"

      MySetup.nsi:
          !include "english.nsh"
  !include "Swedish.nsh"

        Function onAbort
        ${If} $PageId <> 0
            ${If} ${Cmd} ` MessageBox MB_YESNO "${MUI_CANCEL_MESSAGE}" IDYES `
                ;Call WelLeave
                SendMessage $HWNDPARENT 0x408 -$PageId ""
            ${EndIf}
            Abort
        ${EndIf}
    FunctionEnd

然后我更改了 customenglish.nsh 和 Sweedish.nsh 文件,如下所示。这次它正在编译但是当点击“取消”时,在弹出的消息框中它没有显示文本而是显示“${MUI_CANCEL_MESSAGE}”

english.nsh

!ifdef MUI_CANCELWARNING
${LangFileString} MUI_CANCEL_MESSAGE "Are you sure you want to cancel EMR?"
!endif
!insertmacro MUI_LANGUAGE "English"

有多种方法可以做到这一点。

您可以手动覆盖每种语言的每个 MUI 字符串:

!include "MUI2.nsh"
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_INSTFILES

!define MUI_TEXT_WELCOME_INFO_TITLE "English title goes here"
!insertmacro MUI_LANGUAGE "English"

!define MUI_TEXT_WELCOME_INFO_TITLE "Swedish bork bork"
!insertmacro MUI_LANGUAGE "Swedish"

您可以将这些定义放在单独的文件中,!include 如果您希望每种语言有一个覆盖文件,则可以将它们放在单独的文件中。

另一种方法是将所有与语言相关的说明放入您的自定义语言文件中:

MySetup.nsi:

!include "MUI2.nsh"
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_INSTFILES
!include "MyEnglish.nsh"
!include "MySwedish.nsh"

Section
DetailPrint $(myCustomString)
SectionEnd

MySwedish.nsh:

!define MUI_TEXT_WELCOME_INFO_TITLE "Swedish bork bork"
!insertmacro MUI_LANGUAGE "Swedish"
LangString myCustomString 0 "Bork !?"