WinUI3Localizer icon indicating copy to clipboard operation
WinUI3Localizer copied to clipboard

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

Open DexrnZacAttack opened this issue 1 year ago • 3 comments

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.

DexrnZacAttack avatar Jan 13 '25 04:01 DexrnZacAttack

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.

AndrewKeepCoding avatar Jan 14 '25 02:01 AndrewKeepCoding

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.

So I assume it's due to anything with a . in the name? I was trying with names like Packages.GettingAppInfo

DexrnZacAttack avatar Feb 24 '25 08:02 DexrnZacAttack

That . might be the problem. The . is used for properties.

AndrewKeepCoding avatar Feb 24 '25 09:02 AndrewKeepCoding

Closing for now. Feel free to reopen or start a new issue if you need further assistance.

AndrewKeepCoding avatar Oct 21 '25 09:10 AndrewKeepCoding