Reputation: 7324
teaching myself about .Net Core asp.
I have added a resource file to my solution. I have added a test string to this resource file. I am now trying to access this test string/key within my controller.
So, in my startup.cs I have this:
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
services.AddMvc()
.AddViewLocalization(
LanguageViewLocationExpanderFormat.Suffix,
opts => { opts.ResourcesPath = "Resources"; })
.AddDataAnnotationsLocalization();
services.Configure<RequestLocalizationOptions>(
opts =>
{
var supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-GB"),
new CultureInfo("en-US"),
new CultureInfo("en"),
new CultureInfo("fr-FR"),
new CultureInfo("fr"),
};
opts.DefaultRequestCulture = new RequestCulture("en");
// Formatting numbers, dates, etc.
opts.SupportedCultures = supportedCultures;
// UI strings that we have localized.
opts.SupportedUICultures = supportedCultures;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, WorkerContext context)
{
app.UseStaticFiles();
var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(options.Value);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
In my Controller I have this:
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
public HomeController(IStringLocalizer<HomeController> localizer)
{
_localizer = localizer;
}
}
My resource file stored here:
My properties for this resource file are:
I set a breakpoint here:
_localizer = localizer;
I inspect this variable and as you see the manifest has not been found...
What am i not understanding please?
Upvotes: 3
Views: 6927
Reputation: 5746
My resources also didn't work, so I checked the IStringLocalizer.GetAllStrings()
, saw the same error "No manifests exist for the current culture" so went chasing that.
Turns out the only issue was my resx
file was named wrong, it had to add the namespace (minus the assembly name) to the filename. That fixed the resources, they now work fine.
However, even with resources now working, the call to IStringLocalizer.GetAllStrings()
still throws the same exception.
Take home message: That exception doesn't necessarily mean your resources do not work.
The Troubleshoot ASP.NET Core Localization page might also be a helpful resource.
Upvotes: 0
Reputation: 54
It looks like you just described my issue as I went through all of your steps, but it was nothing.
I noticed that localization required an extension class but the DLL for that extension was not in my dependencies. Anyway I used NuGet to install couple of dependencies and my project worked like a charm. Try to install the following and try again.
Microsoft.Extensions.Localization, Microsoft.AspNetCore.Mvc.Localization
Upvotes: 1
Reputation: 12858
Based on https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization
You're supposed to put your Resources
in a folder called Resources under the root. You have it under the Properties
folder...
This code block (slightly different from yours)
Statup.cs
services.AddLocalization(options => options.ResourcesPath = "Resources");
resolves to that folder for me in my projects.
The other issue is your naming isn't done right. Take a look at the section called Working With Resource Files which will do a better job of explaining it to you than me putting it here.
Upvotes: 2