问题
In trying to localize my application, I've followed the steps here: https://docs.asp.net/en/latest/fundamentals/localization.html
Here is my code:
Startup.cs
public List<IRequestCultureProvider> RequestCultureProviders { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.AddViewLocalization(options => options.ResourcesPath = "Resources")
.AddDataAnnotationsLocalization();
services.AddOptions();
services.AddTransient<IViewRenderingService, ViewRenderingService>();
services.AddTransient<IEmailSender, EmailSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(locOptions.Value);
app.UseStaticFiles();
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory())),
EnableDirectoryBrowsing = true
});
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr"),
};
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("fr"),
// Formatting numbers, dates, etc.
SupportedCultures = supportedCultures,
// UI strings that we have localized.
SupportedUICultures = supportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider
{
QueryStringKey = "culture",
UIQueryStringKey = "ui-culture"
}
}
});
}
MyController.cs
public class MyController : Controller
{
private readonly IViewRenderingService _viewRenderingService;
private IStringLocalizer<MyController> _localizer;
private MyOptions _options;
//Constructor for dependency injection principle
public MyController(IViewRenderingService viewRenderingService, IStringLocalizer<MyController> localizer, IOptions<MyOptions> options)
{
_viewRenderingService = viewRenderingService;
_localizer = localizer;
_options = options.Value;
}
[HttpGet]
public string Get()
{
// _localizer["Name"]
return _localizer["Product"];
}
}
The *.resx
file is stored in the Resources
folder, with the name Controllers.MyController.fr.resx
(which has an entry for "Product").
However, it's not able to find the resource file, and "Product" is never returned in French. I am using querystring, so here is the query string:
localhost:3333/my?culture=fr
Also in the View, @Localizer["Product"]
returns "Product".
Can anyone please help me find whats missing?
EDIT:
After some investigation, I found that culture is getting changed, however it is unable to locate the Resource file. I am using VS2015. can anyone help?
回答1:
I had similar problem. Than I figured out that the "Localization.AspNetCore.TagHelpers" nuget packag was missing from my project. It's look like it is a required package for the QueryStringRequestCultureProvider.
回答2:
You need to add these references from nuget
:
Microsoft.Extensions.Localization
and also Localization.AspNetCore.TagHelpers
can be a good tag helper instead of injecting the localization stuffs every time to the views
回答3:
For me it was a problem that the name of the project's folder was different than the root namespace! Core 3.0.
回答4:
By default, Visual studio IDE resolves the assembly references, but it requires the Microsoft.Extensions.Localization and Microsoft.Extensions.Localization.Abstractions NuGets. Once I added them to the project reference, the resource locator is able to find the resource files!
回答5:
I had the same problem in .net core 2.2 i see in the debugger the properties SearchedLocation
so, i created first the files Controllers.HomesController.rsx without language extension with access modifier "Public" to accces to the property
and with localizer["Property] i found the goood value.
After i created the Controllers.HomeController.fr.resx and he found the good value with culture in url.
回答6:
I am using VS 2017 targeting .NET Core 2.1 and in my case the project file (.csproj) contained a few strange ItemGroup tags, like these:
<ItemGroup>
<EmbeddedResource Remove="Resources\Controllers.HomeController.sv.resx" />
...
</ItemGroup>
<ItemGroup>
<None Include="Resources\Controllers.HomeController.sv.resx" />
...
</ItemGroup>
When I deleted the lines it started working.
I had been experimenting earlier with adding and removing the resource files so that might have caused the item groups to appear, but it's strange that Visual Studio inserted these lines at all. Seems like a bug.
回答7:
I had the same problem and I fixed it by removing the built-in providers in order to be able to use the default request culture. For do that you'll need to add this code to your services.Configure in the startup class:
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
Please take a look to this other answer: ASP .NET Core default language is always English
回答8:
If the root namespace of an assembly is different than the assembly name:
Localization does not work by default. Localization fails due to the way resources are searched for within the assembly. RootNamespace is a build-time value which is not available to the executing process.
If the RootNamespace is different from the AssemblyName, include the following in AssemblyInfo.cs (with parameter values replaced with the actual values):
using System.Reflection;
using Microsoft.Extensions.Localization;
[assembly: ResourceLocation("Resource Folder Name")]
[assembly: RootNamespace("App Root Namespace")]
More info here
来源:https://stackoverflow.com/questions/39132868/localization-in-asp-net-core-mvc-not-working-unable-to-locate-resource-file