Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error building the localizer #54

Open
LluisV opened this issue Dec 10, 2024 · 9 comments
Open

Error building the localizer #54

LluisV opened this issue Dec 10, 2024 · 9 comments

Comments

@LluisV
Copy link

LluisV commented Dec 10, 2024

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.

@AndrewKeepCoding
Copy link
Owner

AndrewKeepCoding commented Dec 10, 2024

Hi @LluisV
The exception is coming from CreateLanguageDictionaryItem, so it seems that there is something wrong in the *.resw file. 🤔

@LluisV
Copy link
Author

LluisV commented Dec 10, 2024

@AndrewKeepCoding

Thank you for your quick response!

It seems that there is something wrong in the *.resw file. 🤔

I have asked them to send me a copy of their .resw file. It is identical to the one I have.
It's strange because the app has thousands of users and this is the first one this has happened to. They told me that they reinstalled the app and the error persists.

@AndrewKeepCoding
Copy link
Owner

AndrewKeepCoding commented Dec 10, 2024

The only way that I can reproduce this exception message is by setting a negative value in firstSeparatorIndex before AsSpan is called. But I can't come up with a case that firstSeparatorIndex could be less than 0 or larger than the text length.

internal static LanguageDictionary.Item CreateLanguageDictionaryItem(string name, string value)
{
    (string Uid, string DependencyPropertyName) = name.IndexOf(".") is int firstSeparatorIndex && firstSeparatorIndex > 1
        ? (name[..firstSeparatorIndex], string.Concat(name.AsSpan(firstSeparatorIndex + 1), "Property"))
        : (name, string.Empty);
    return new LanguageDictionary.Item(
        Uid,
        DependencyPropertyName,
        value,
        name);
}
// Exceptions:
//   T:System.ArgumentNullException:
//     text is null.
//
//   T:System.ArgumentOutOfRangeException:
//     start is less than 0 or greater than text.Length.
public static ReadOnlySpan<char> AsSpan(this string? text, int start);

@AndrewKeepCoding
Copy link
Owner

I supposed this used to work, right? Can your user try installing the app on another system?

@LluisV
Copy link
Author

LluisV commented Dec 10, 2024

@AndrewKeepCoding Yes, the app has thousands of users (Windows x64 only), and this is the first one to report this error. Unfortunately he says he doesn't have access to another system.

@AndrewKeepCoding
Copy link
Owner

AndrewKeepCoding commented Dec 10, 2024

Is the exception gone if you pass an *.resw file with no string resources?
If so, might take time but can you try reducing the strings by chunks to narrow it down which string is causing the issue?

@LluisV
Copy link
Author

LluisV commented Dec 11, 2024

@AndrewKeepCoding I just realized that I was using a 1.x version of the library. Is it possible that this bug was fixed in later versions? I see that the code for CreateLanguageDictionaryItem has changed.
The problem is that in order to upgrade to version 2.x I need to upgrade the Windows SDK version, which introduces a bug in the materials that use the acrylic backdrop (you know, WinUI 3 stuff). Anyway, I'll keep you updated as soon as I get more info. Thanks for your help!

Edit: still happening in version 2.2

@LluisV
Copy link
Author

LluisV commented Dec 11, 2024

Update: a Resources.resw with just 1 string still fails.

@AndrewKeepCoding
Copy link
Owner

This seems to be a very rare case, and I have no idea what might be causing this issue.
If you are up to it, I guess it's faster if you just clone the repo, edit the code (add some logging or throw exceptions), create a test package and locally install it to your app. I can help you out with the packaging process if you are not familiar with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants