oleg-shilo / wixsharp

Framework for building a complete MSI or WiX source code by using script files written with C# syntax.
MIT License
1.12k stars 175 forks source link

Dynamic localization via custom dialog #1564

Closed kipamgs closed 4 months ago

kipamgs commented 5 months ago

Hi, I added a combobox to the welcome dialog so the user can choose the language of the installer. I tried doing it in a similar way to the MultiLanguagesUI Example .

I would like to switch the languages if a user chooses another language of the combobox. The ideal way would be to load the localized strings in the ComboBox_SelectionChanged method.

Once I selected another language via runtime.UIText.InitFromWxlhow can I switch back to english?

image

        private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {

            try
            {
             var runtime = Host?.Shell.MsiRuntime(); // Host?.MsiRuntime();
             switch((SupportedLanguages) LanguageCombobox.SelectedIndex)
             {
                 case SupportedLanguages.German:
                     runtime.UIText.InitFromWxl(Host?.Session().ReadBinary("de_xsl"));
                     break;
                 case SupportedLanguages.English:
                     // How can i switch back to english?
                     break;
             }
            }
            catch (System.Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
oleg-shilo commented 5 months ago

Sorry for the delayed response.

The English localization is stored in the same table where other localizations are, though under the WixSharp_UIText key. image

The English localization is always stored there regardless if you embed other languages or not.

At runtime you need to update the localization between dialogs. You do this by subscribing to the UI events that are triggered on the dialog change. Since you want to re-localize the second dialog then you need to do that right in the UILoaded event handler:

project.UILoaded += (SetupEventArgs e) =>
        {
            // first dialog is loaded
            MsiRuntime runtime = e.ManagedUI.Shell.MsiRuntime();
            runtime.UIText.InitFromWxl(e.Session.ReadBinary("WixSharp_UIText"));

            e.ManagedUI.OnCurrentDialogChanged += (IManagedDialog obj) =>
            {
                // any dialog after the first one is loaded
                // you can also check the dialog type here
            };
        };
kipamgs commented 4 months ago

Thank you