AndrewKeepCoding / WinUI3Localizer

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

Minimal, simple example usage. #5

Closed p6laris closed 1 year ago

p6laris commented 1 year ago

Hello There, Is it possible to provide a minimal, beginner-friendly example usage. I have carefully examined the code samples available in the repository, but as a beginner, I still find it challenging to comprehend the implementation details.

Thank you for the efforts

AndrewKeepCoding commented 1 year ago

Hi! Is your app a packaged app (default) ? or a non-packaged app?

p6laris commented 1 year ago

It's is packaged app sir, Generated with Template Studio.

AndrewKeepCoding commented 1 year ago

I'm working on a sample app generated with the TemplateStudio. Just give me a few days🔥

p6laris commented 1 year ago

Thanks, appreciated so much 🙏

AndrewKeepCoding commented 1 year ago

Sorry for the delay. This is a sample app is created with the TemplateStudio. Let me know if you have any questions.

TemplateStudioWinUI3LocalizerSampleApp

p6laris commented 1 year ago

Sir, When i try to initialize the localizer, strangely this exception will throw. any idea why? WinUI3Localizer.LocalizerException: 'Failed to set language. [{PreviousLanguage} -> {NextLanguage}]'

AndrewKeepCoding commented 1 year ago

Sorry. That exception message is going to be fixed in the next version. You might get this message when the SetLanguage(string language) method is called but failed to find the language.

This is how the SetLanguage method is implemented inside the WinUI3Localizer.

    public async Task SetLanguage(string language)
    {
        string previousLanguage = CurrentDictionary.Language;

        try
        {
            if (this.languageDictionaries.TryGetValue(
                language,
                out LanguageDictionary? dictionary) is true &&
                dictionary is not null)
            {
                CurrentDictionary = dictionary;
                await LocalizeDependencyObjects();
                OnLanguageChanged(previousLanguage, CurrentDictionary.Language);
                return;
            }
        }
        catch (LocalizerException)
        {
            throw;
        }
        catch (Exception exception)
        {
            ThrowLocalizerException(exception, "Exception setting language. [{PreviousLanguage} -> {NextLanguage}]", previousLanguage, language);
        }

        ThrowLocalizerException(innerException: null, "Failed to set language. [{PreviousLanguage} -> {NextLanguage}]", previousLanguage, language);
        return;
    }

My first guess is that you are not setting the DefaultLanguage in the options:

ILocalizer localizer = await new LocalizerBuilder()
    .SetOptions(options =>
    {
        options.DefaultLanguage = "YOUR DEFAULT LANGUAGE";
    }
    .Build();

You also need to make sure that your default language is added as a LanguageDictionary.