I am using NSIS to create the installer. While creating the installer I need to customize the screens based on my requirement. I got to know that we should not modify the NSIS included .NSH files.
So, I am doing it like as shown below: But here I need to do it for around 9 languages. Is there any better way to segregate separate file for each language and use it here? And also how to add buttons and checkbox as part of the customization to the License Agreement screen?
;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
Updated Code with two separate .nsh files (CustomChinese.nsh and CustomEnglish.nsh):
As per your inputs, I kept the !define statements per language in separate .nsh files (CustomChinese.nsh and CustomEnglish.nsh) and then include those files in my .nsi file.
But it is not allowing to include the two .nsh files (CustomChinese.nsh and CustomEnglish.nsh) and two language files (!insertmacro MUI_LANGUAGE "English" and !insertmacro MUI_LANGUAGE "Chinese") simultaneously. So, I have given only one file each. But how to give the multiple files so that based on the locale it will take the appropriate file?
Below is the complete code:
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 $0 $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 *$1(i.r2,i.r3,i.r4,i.r5)
IntOp $5 $5 - $3 ;height
IntOp $4 $4 - $2 ;width
System::Call 'USER32::ScreenToClient(p$hwndparent,pr1)'
System::Call *$1(i.r2,i.r3)
System::Free $1
IntOp $2 $4 / 5 ; Calculate x padding based on the width but you can put any value you want in $2
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 $0
SendMessage $hwndparent ${WM_GETFONT} 0 0 $1
SendMessage $0 ${WM_SETFONT} $1 1
GetFunctionAddress $0 onmybtnclick
ButtonEvent::AddEventHandler 0x666 $0
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"
Below is the Updated Code snippet with !ifdef statements in both the .nsh files:
Even for me the sample is working. But If I include MUI attributes for eg (MUI_CANCEL_MESSAGE "Are you sure you want to cancel?" in both "MyEnglish.nsh" and "MySwedish.nsh" it is showing the compiler error: !define: "MUI_CANCEL_MESSAGE" already defined!
Below is the Code:
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
Then I changed the customenglish.nsh and Sweedish.nsh files like as shown below. This time it is compiling but when clicked on "Cancel", in the message box that is popping-up it is not showing the text instead it is showing "${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"
There are multiple ways to do this.
You can override each MUI string for each language manually:
!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"
You can put those defines in separate files and !include
them if you want one override file per language.
Another method is to put all the language related instructions in your custom language files:
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 !?"