Issues getting strings that are clearly there (even seen in a breakpoint)
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.
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,
// 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.
This should work. I just confirmed with a test app. I guess the issue is in the
nameargument you are passing toGetLocalizedString.For example,
// 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
F11to 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
That . might be the problem. The . is used for properties.
Closing for now. Feel free to reopen or start a new issue if you need further assistance.