WinUI3Localizer icon indicating copy to clipboard operation
WinUI3Localizer copied to clipboard

Error building the localizer

Open LluisV opened this issue 10 months ago • 9 comments

Hey, a client sent me this screenshot, which corresponds to this code snippet. I've been trying to fix it for a couple of hours, but I can't figure out what the problem is.

image


public App()
{
    Instance = this;
    this.InitializeComponent();
    InitializeLocalizerAsync();
    Settings.LoadConfig();
}

private async void InitializeLocalizerAsync()
{
    await InitializeLocalizer();
}

public async Task InitializeLocalizer()
{
    try
    {
        // Initialize a "Strings" folder in the executables folder.
        var StringsFolderPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Strings");

        // Ensure the folder exists
        if (!Directory.Exists(StringsFolderPath))
        {
            MessageBox.Show($"ERROR: Strings folder does not exist at path: {StringsFolderPath}");
            return;
        }

        StorageFolder stringsFolder = await StorageFolder.GetFolderFromPathAsync(StringsFolderPath);

        var currentLanguage = Settings.GetCurrentLanguage();
        if (string.IsNullOrWhiteSpace(currentLanguage))
        {
            MessageBox.Show("Invalid current language specified.");
            currentLanguage = "en-US";
        }

        ILocalizer localizer = await new LocalizerBuilder()
            .AddStringResourcesFolderForLanguageDictionaries(StringsFolderPath)
            .SetOptions(options =>
            {
                options.DefaultLanguage = currentLanguage;
            })
            .Build();

        try
        {
            var languageDictionary = localizer.GetCurrentLanguageDictionary();
            if (languageDictionary == null)
            {
                MessageBox.Show("ERROR: Language dictionary is null.");
                return;
            }

            language_items = languageDictionary.GetItems().ToList();
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERROR 5.1: " + ex.Message + "\nDetails: " + ex.StackTrace);
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show($"ERROR 5: {ex.Message}\nDetails: {ex.StackTrace}");
    }
}
  • We know that the directory exists because that error message is not displayed.
  • We know that the try catch block corresponding to the languageDictionary check is not executed, meaning that the error is before that.
  • I have tried disabling the exceptions produced by AddStringResourcesFolderForLanguageDictionaries, but then of course I don't get the strings.

Do you know where the error could come from?

Thank you in advance for your time.

LluisV avatar Dec 10 '24 11:12 LluisV