Search code examples
nsis

NSIS: Use selected language for MessageBox in .onInit (MUI2)


I try to get a localized message box in the .onInit method which fails with the following code:

!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "German"

LangString Message ${LANG_ENGLISH} "This is a message."
LangString Message ${LANG_GERMAN} "Dies ist eine Nachricht"

Function .onInit
  !insertmacro MUI_LANGDLL_DISPLAY
  MessageBox MB_OK "$(Message)"
FunctionEnd

The MessageBox always shows the same language string.


Solution

  • The Problem is, that the language is processed after the .onInit method.

    A workaround for this could be to put the custom code from the .onInit method to the .onGUIInit method.

    With MUI2 this is done as follows:

    !define MUI_CUSTOMFUNCTION_GUIINIT myGuiInit
    
    !include "MUI2.nsh"
    
    !insertmacro MUI_LANGUAGE "English"
    !insertmacro MUI_LANGUAGE "German"
    
    LangString Message ${LANG_ENGLISH} "This is a message."
    LangString Message ${LANG_GERMAN} "Dies ist eine Nachricht"
    
    Function .onInit
      !insertmacro MUI_LANGDLL_DISPLAY
    FunctionEnd
    
    Function myGuiInit
      MessageBox MB_OK "$(Message)"
    FunctionEnd
    

    Now the MessageBox should show the correctly localized message.