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

Issues getting strings that are clearly there (even seen in a breakpoint) #55

Open
DexrnZacAttack opened this issue Jan 13, 2025 · 1 comment

Comments

@DexrnZacAttack
Copy link

My setup looks a bit like this:
Strings/en-US/Resources.resw

under app.xaml.cs:

        private static async Task InitializeLocalizer()
        {
            string StringsFolderPath = Path.Combine(AppContext.BaseDirectory, "Strings");
            StorageFolder stringsFolder = await StorageFolder.GetFolderFromPathAsync(StringsFolderPath);

            ILocalizer localizer = await new LocalizerBuilder()
                .AddStringResourcesFolderForLanguageDictionaries(StringsFolderPath)
                .SetOptions(options =>
                {
                    options.DefaultLanguage = "en-US";
                })
                .Build();
            Logger.WriteDebug($"Using language {localizer.GetCurrentLanguage()}");
        }

        protected override async void OnLaunched(LaunchActivatedEventArgs args)
        {
            await InitializeLocalizer();
            Logger.WriteDebug("Showing MainWindow");
            MainWindow.Activate();
        }

There's a helper that I often call, which does this:

        public static string GetLocalizedText(string name, params object[] args)
        {
            String translated = Localizer.Get().GetLocalizedString(name);
            if (translated == "")
                Logger.Write(LogLevel.Warning, $"String {name} not found in string resources.");
            return string.Format(translated == "" ? $"LOCALIZATION ERROR: String {name} not found in string resources." : translated, args);
        }

When stepping through the application, I can actually see the strings under the ILocalizer, but it always returns String.Empty in the end.
The app is unpackaged, and I'm not sure if this is enough information so please lmk if you need more.

@AndrewKeepCoding
Copy link
Owner

This should work. I just confirmed with a test app.
I guess the issue is in the name argument you are passing to GetLocalizedString.

For example,

image

// Returns "Some text."
Localizer.Get().GetLocalizedString("SomeText");

// Returns ""
Localizer.Get().GetLocalizedString("SomeButton.Content");

// Returns "Hello!"
Localizer.Get().GetLocalizedString("SomeButton");

Also, you can also debug deeper into the WinUI3Localizer with F11 to see why you are getting an empty string.

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