如何根据语言环境加载文件并使用 NSIS 生成安装程序?

How to load the files based on the locale and generate the installer using NSIS?

我的要求是,我有每个区域设置(语言)的特定文件(Dll、chms 等)。我需要根据语言环境加载这些文件并生成安装程序。 卸载时我也应该从 traget 目录中卸载这些文件。

这里我正在做的是在 .onInit 函数中,使用 GetUserDefaultUILanguage() 我正在获取语言环境并检查该语言环境并加载该语言环境下的文件。

这是正确的方法吗?请为此代码提供任何建议。

我们还需要在“语言”部分之前使用“页面”部分吗?

因为我在编译时收到警告,要求在 ;Languages 部分之前使用 ;Pages 部分。

下面是我写的代码片段:

; LocaleDlls.nsi
;
;
; It will install LocaleDlls.nsi into a directory that the user selects.

;--------------------------------
!include LogicLib.nsh
 !include "MUI2.nsh"
; The name of the installer in the path C:\Program Files\LocaleDlls
Name "LocaleDlls"

; The file to write  in the path E:\Source\NULLSOFT\src
OutFile "LocaleDlls.exe"

; The default installation directory in the path C:\Program Files\LocaleDlls
InstallDir $PROGRAMFILES\LocaleDlls

; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically) It shows the path the path C:\Program Files\LocaleDlls
InstallDirRegKey HKLM "Software\NSIS_LocaleDlls" "Install_Dir"

; Request application privileges for Windows Vista
RequestExecutionLevel admin

;--------------------------------

; Pages

Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;Pages

; Do we need to use PAGE macros before giving LANGUAGE as when compiling we are getting an error.
;--------------------------------

;Languages

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

;--------------------------------

;Installer Functions

Function .onInit

 System::Call 'kernel32::GetUserDefaultUILanguage() i.r10'
 MessageBox MB_OK "Return value = $R0"
 
 StrCpy $Language ${LANG_PORTUGUESEBR}
 MessageBox MB_OK "Return value = $Language"
 
 ${If} $Language P= 1046

  MessageBox MB_OK "Current Locale is Portuguese... Loading Portuguese Files"
  ${EndIf}
  File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\AllowStandby.reg
  File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\Test.chm
  File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\Testdlg.dll
  File E:\Source\NULLSOFT\src\EngPortuguese\Portuguese\resource.dll
  
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


  System::Call 'KERNEL32::LoadLibrary(t "$INSTDIR\testdlg.dll.dll")p.r8 ?e'
  Pop  ; Get ?e result
  ${IfThen}  P= 0 ${|} MessageBox MB_ICONSTOP "Failed to load pchuteres.dll, error " ${|}

  ${If}  P<> 0
  MessageBox MB_OK 'Successfully loaded "$INSTDIR\testdlg.dll.dll" @ '
  
  ${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

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\LocaleDlls"
  CreateShortcut "$SMPROGRAMS\LocaleDlls\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortcut "$SMPROGRAMS\LocaleDlls\LocaleDlls (MakeNSISW).lnk" "$INSTDIR\LocaleDlls.nsi" "" "$INSTDIR\LocaleDlls.nsi" 0

SectionEnd

;--------------------------------

; Uninstaller

Section "Uninstall"

  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\LocaleDlls"
  DeleteRegKey HKLM SOFTWARE\NSIS_LocaleDlls

  ; Remove files and uninstaller
  Delete $INSTDIR\LocaleDlls.nsi
  Delete $INSTDIR\uninstall.exe

  ; Remove shortcuts, if any
  Delete "$SMPROGRAMS\LocaleDlls\*.*"

  ; Remove directories used
  RMDir "$SMPROGRAMS\LocaleDlls"
  RMDir "$INSTDIR"

SectionEnd
;--------------------------------
;Uninstaller Functions

Function un.onInit

  !insertmacro MUI_UNGETLANGUAGE

FunctionEnd

您不需要调用 GetUserDefaultUILanguage,NSIS 调用 GetUserDefaultUILanguage 尝试设置默认值 $language。如果找不到匹配的语言,则使用 .NSI 中指定的第一种语言。所有这些都发生在调用 .onInit 之前,您无需执行任何操作。但是,如果您对 NSIS 选择的语言不满意,可以在 .onInit 中更改 $language。您还可以使用 !insertmacro MUI_LANGDLL_DISPLAY 显示语言选择对话框。

使用MUI时需要在语言之前插入页面,因为语言宏需要知道页面需要哪些字符串。使用 MUI 页面宏 MUI_PAGE_* 而不是本机页面很重要:

!insertmacro MUI_PAGE_WELCOME
Page Custom MyPage ; There is no MUI_PAGE_* macro for this
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English" ; Must come after all MUI_PAGE_* macros.