Localization in ASP.Net core MVC not working - unable to locate resource file

萝らか妹 提交于 2019-12-03 23:56:56

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.

Shervin Montajam

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

For me it was a problem that the name of the project's folder was different than the root namespace! Core 3.0.

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.

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!

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!