I'm trying to implement localization in my ASP.NET Core application using VS Code, but I keep getting "No resources found = true". I've double-checked my implementation and I'm pretty sure everything is set up correctly, but I can't figure out what's causing this.
Here's what I've done so far:
I've added the Microsoft.Extensions.Localization package to my project. I've created a Resources folder with a x.en-US.resx file in it for my default language (English). I've created a x.fr-FR.resx file in the same folder for French translations. I Use ResX Editor Extension to create resx files. In my Program.cs file, I've added the following lines of code to configure localization:
builder.services.AddLocalization(options => options.ResourcesPath = "Resources");
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR"),
};
var options = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
app.UseRequestLocalization(options);
In my controller, I'm using the IstringLocalizer to translate my localized strings, like this:
private readonly IStringLocalizer<WeatherForecastController> _localizer;
public WeatherForecastController(IStringLocalizer<WeatherForecastController> localizer)
{
_localizer = localizer;
}
[HttpGet]
public IActionResult Get()
{
return Ok(_localizer["Key"]) ;
}
i have added the key and its value to resix file
Despite all this, I still get the "No resources found". Can anyone point me in the right direction to solve this issue?
Thanks in advance for any help!
It seems like your naming is off. The path or name of the resx files must match the namespace of the type given as type argument to IStringLocalizer<T>
.
Assuming your class and resx files aren't actually named 'x', both these naming conventions/folder structures will work.
Resources/
├── Controllers.WeatherForecastController.en-US.resx
└── Controllers.WeatherForecastController.fr-FR.resx
or
Resources/
└── Controllers
├── WeatherForecastController.en-US.resx
└── WeatherForecastController.fr-FR.resx