0

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

4
  • Please don't leave the default text “enter image description here”, always change it to some descriptive image title. Please don't post textual information in the form of pictures. Please see: Why should I not upload images of code/data/errors?. If you still need an image, it can complement the textual information. Commented Feb 25 at 14:13
  • Please clarify: what is the exact output of Console.WriteLine($"Direct ResourceManager lookup: {localizedValue}");? Commented Feb 25 at 14:15
  • 1
    if CultureInfo("en-US"): Direct ResourceManager lookup: Can't edit closed order. if CultureInfo("uk-UA") Direct ResourceManager lookup: Не можна редагувати закрите замовлення. Commented Feb 25 at 14:22
  • Thank you for the clarification, «Не можна редагувати...» :-) Commented Feb 25 at 14:54

1 Answer 1

0

From Configure localization services,

AddLocalization adds the localization services to the services container, including implementations for IStringLocalizer<T> and IStringLocalizerFactory. The preceding code also sets the resources path to "Resources".

Currently, all your resources files are located in the another assembly with path: Application\ErrorMessages but not under the API project.

Hence, you only specify options.ResourcesPath when the resources files are in the same project and fall under the folder.

For the illustration of using the ResourcePath, your project structure should be as below:

Solution
├── Application
|
└── API
    ├── Resources
    |   └── ErrorMessages
    |       ├── ErrorMessages.resx
    |       └── ErrorMessages.uk-UA.resx
    └── Program.cs

This should allow to scan the resources files in the all assemblies with pre-requisites that you already complete adding the Application project (reference) to API project.

builder.Services.AddLocalization();

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.