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

时间:2018-10-24 14:29:53

标签: 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:\Code\PCPE\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:\Code\PCPE\NULLSOFT\src\EngPortuguese\Portuguese\AllowStandby.reg
  ;File E:\Code\PCPE\NULLSOFT\src\EngPortuguese\Portuguese\APC PCPE.chm
  File E:\Code\PCPE\NULLSOFT\src\EngPortuguese\Portuguese\pchuteres.dll
  File E:\Code\PCPE\NULLSOFT\src\EngPortuguese\Portuguese\res.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\pchuteres.dll.dll")p.r8 ?e'
  Pop $7 ; Get ?e result
  ${IfThen} $8 P= 0 ${|} MessageBox MB_ICONSTOP "Failed to load pchuteres.dll, error $7" ${|}

  ${If} $8 P<> 0
  MessageBox MB_OK 'Successfully loaded "$INSTDIR\pchuteres.dll.dll" @ $8'

  ${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

1 个答案:

答案 0 :(得分:1)

您不需要致电GetUserDefaultUILanguage,NSIS会致电GetUserDefaultUILanguage来尝试设置默认的$language。如果找不到匹配的语言,则使用.NSI中指定的第一种语言。所有这些都在调用.onInit之前发生,您无需执行任何操作。但是,如果您对NSIS选择的语言不满意,可以在$language中更改.onInit。您还可以使用!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.