Example of values in ErrorMessages.resx
Or example in text
name: "OrderIsClosed" neutral value: "Can't edit closed order." uk-UA: "Не можна редагувати закрите замовлення."
I'm sending a request through Postman to my API, and I expect to receive a localized error message, for example, "Can't edit closed order.". However, instead, I get only the error key in the response, as shown below:
{
"Errors": [
"OrderIsClosed"
]
}
I am using ResourceManager to retrieve localized strings and have configured localization for the desired language. However, when trying to access the localized string, the response only contains the error key, not the actual message.
I've correctly set up the resource files (.resx), and they are placed in the correct project folder with the Build Action set to Embedded Resource.
Could anyone help me understand why the response only returns the error key and not its localized value?
Structure of my project
Solution
├── Application
│ └── ErrorMessages
│ | ├── ErrorMessages.resx
│ | └── ErrorMessages.uk-UA.resx
| └── OrderService
|
|
└── API
└── Program.cs
Program.cs
builder.Services.AddLocalization(options => options.ResourcesPath = "ErrorMessages");
builder.Services.AddSingleton(new ResourceManager("Application.ErrorMessages.ErrorMessages",
typeof(Program).GetTypeInfo().Assembly));
var supportedCultures = new[] { "en-US", "uk-UA" };
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture("en-US")
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
OrderService
if (order.IsClosed())
return ServiceResults.Failed(_localizer["OrderIsClosed"].Value);
Tryed to test it with this code
var resourceManager = new ResourceManager("Application.ErrorMessages.ErrorMessages", Assembly.GetExecutingAssembly());
string localizedValue = resourceManager.GetString("OrderIsClosed", new CultureInfo("uk-UA"));
Console.WriteLine($"Direct ResourceManager lookup: {localizedValue}");
and it works, but i have no clue why it does not work with regular solution
Console.WriteLine($"Direct ResourceManager lookup: {localizedValue}");?