AndrewKeepCoding / WinUI3Localizer

The WinUI3Localizer is a NuGet package that helps you localize your WinUI 3 app.
MIT License
85 stars 6 forks source link

MVVM sample project in NuGet Gallery crash with V2 #43

Closed gmgallo closed 5 months ago

gmgallo commented 6 months ago

I'm implementing the WinUI3 Localizer in my MVVM project duplicating the sample project available with the NuGet package. Unfortunately, SettingsViewModel.cs throws and exception when its constructor reaches the last statement because _localizationService.GetCurrentLanguage() returns an empty string;

    public SettingsViewModel(IThemeSelectorService themeSelectorService, ILocalizationService localizationService)
    {
        _themeSelectorService = themeSelectorService;
        _elementTheme = _themeSelectorService.Theme;
        _versionDescription = GetVersionDescription();

        SwitchThemeCommand = new RelayCommand<ElementTheme>(
            async (param) =>
            {
                if (ElementTheme != param)
                {
                    ElementTheme = param;
                    await _themeSelectorService.SetThemeAsync(param);
                }
            });

        _localizationService = localizationService;
        AvailableLanguages = _localizationService.GetAvailableLanguages()
            .Select(x => new LanguageItem(Language: x, UidKey: $"{"Settings_Language"}_{x}"))
            .ToList();

       // next line throws an exception because  _localizationService.GetCurrentLanguage() returns an empty string "";
        _selectedLanguage = AvailableLanguages.First(x => x.Language == _localizationService.GetCurrentLanguage());
    }

the second problem I encountered is that the It fails to return the default language in a machine where the app runs for a first time (ie no "AppLocalizationLanguage" setting yet). This was solved by preventing that to changing the initalization logic in LocalizationService.cs from this:

    public async Task InitializeAsync()
    {
        await InitializeLocalizer();
        if (await LoadLanguageFromSettingsAsync() is string language)
        {
            await Localizer.SetLanguage(language);
        }
    }

To this:

    public async Task InitializeAsync()
    {
        await InitializeLocalizer();
        if (await LoadLanguageFromSettingsAsync() is string language)
        {
            await Localizer.SetLanguage(language);
        }
        else
        {
            SetLanguageAsync("us-en");
        }
    }

Edit: Whit this change I hopped to be able to get back the localized strings with the default language, but I did not, and _localizationService.GetCurrentLanguage() still returns an empty string.

Maybe I missed to copy a piece of code somewhere?

Implementing the WinUI3Localizer is a great addition to the UX but it is not trivial. Hopefully it gets added to the CommunityToolkit.Mvvm, or, at least a detailed step by step instructions are published with the package for that framework.

gmgallo commented 6 months ago

Also noted that updating the sample project project from MicrosoftWindowsAppSDK version 1.3 to 1.5 crashes at startup.

AndrewKeepCoding commented 6 months ago

Hi, @gmgallo ! I'm going to be away from my laptop, so I won't be able to help you out with this for a couple of days, but can you check the following?

gmgallo commented 6 months ago

Hi @AndrewKeepCoding, Thanks for the quick return!

I'll revert the changes temporarily until you have some time to look into it.

AndrewKeepCoding commented 5 months ago

I was able to reproduce the issue by just updating to WinAppSDK v1.5. It seems that this is not related to the WinUI3Localizer but to the TemplateStudio project.

The error disappears by bringing a PublishProfiles folder and its contents (*.pubxml files) from another working WinAppSDK v1.5. Recreating a TemplateStudio project from scratch might be a better idea.

gmgallo commented 5 months ago

Hi Andrew, thanks for the return, unfortunately the solution is a risky alternative for me. My project is in the final phase with almost two years of development, countless services and views. Too late to start from scratch a new TemplateStudio and migrate everything over there. I have bad experience trying to recycle parts of old project into a new one. Lots of things go on behind the scenes that can't be fixed just by renaming namespaces.
This is a nice feature for many developers around the world. I hope this makes its way as an add in option to TemplateStudio or the CommunityToolkit MVVM templates.

AndrewKeepCoding commented 5 months ago

Closing this since it seems not to be related to WinUI3Localizer. Reopen this or create a new issue if you need my help.