One of my classes in an assembly uses an IStringLocalizer to get strings for a specific language. Strings are stored in .resx files. When I run the service (a BackgroundService) from another class where it's added as a hosted service (services.AddHostedService(sp => ...)), this all works absolutely fine. But when it's loaded by the host using an AssemblyLoadContext, it only returns the language from the default .resx file.
Service is set up like this:
var builder = Host.CreateDefaultBuilder()
.ConfigureServices((ctx, services) =>
{
services.AddLocalization();
...
}
builder.Build();
I have files Localization.resx and Localization.nl.resx, with build action set to Embedded Resource and a Localization.Designer.cs.
Consider the following code of a Domain class:
public Domain(IStringLocalizer<Localization> localizer)
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("nl");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl");
var localizedString = localizer[nameof(Localization.Created)];
}
The value of localizedString is always its English value, from the file Localization.resx. This means this file is being read (the string is stored nowhere else) and IStringLocalizer actually does something: it looks up the string, but it doesn't localize. Only when used in an AssemblyLoadContext.
I tried different configurations in the .resx properties, with Custom tool set to (Public)ResXFileCodeGenerator, or deleting the .Designer.cs file, as I have 2 examples (outside AssemblyLoadContext) and one of them does have the designer file and the other one doesn't. Explicitly setting Localization.Culture = new CultureInfo("nl") also doesn't help. Also, Localization.ResourceManager.GetString("Created"); returns the English string.