Localization in ASP.Net core MVC does not work - cannot find resource file

Trying to localize my application, I did the following: 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 is stored in the Resources folder under the name Controllers.MyController.fr.resx (it has an entry for "Product" in it).

However, it cannot find the resource file, and the Product is never returned in French. I use the query string, so here is the query string:

 localhost:3333/my?culture=fr 

Also in the view, @Localizer["Product"] returns "Product".

Can someone please help me find what is missing?

EDIT:

After some research, I found that the culture is changing, but it cannot find the resource file. I am using VS2015. can anyone help?

+12
source share
8 answers

I had a similar problem. Than I realized that the " Localization.AspNetCore.TagHelpers " nuget packag was missing from my project. This seems to be the required package for QueryStringRequestCultureProvider.

+27
source

You need to add these links from nuget :

 Microsoft.Extensions.Localization 

as well as Localization.AspNetCore.TagHelpers can be a good tag helper instead of typing localization materials into views every time

+2
source

For me it was a problem that the project folder name was different from the root namespace! Core 3.0

+1
source

By default, the Visual Studio IDE allows assembly references, but this requires NuGets Microsoft.Extensions.Localization and Microsoft.Extensions.Localization.Abstractions. After I added them to the project link, the resource locator can find the resource files!

0
source

I had the same problem in .net core 2.2. I see SearchedLocation properties in the debugger enter image description here

So, I first created the Controllers.HomesController.rsx files without a language extension with the "Public" access modifier to access the property enter image description here

and using localizer ["Property] I found the value goood.

After I created Controllers.HomeController.fr.resx and it found good value with culture in the URL.

0
source

I had the same problem and fixed it by removing the built-in providers so that I could use the default query culture. To do this, you need to add this code to your services. Set up in the launch class:

  options.RequestCultureProviders = new List<IRequestCultureProvider> { new QueryStringRequestCultureProvider(), new CookieRequestCultureProvider() }; 

Please look at this other answer: ASP.NET Core is always English by default

0
source

If the root namespace of the assembly is different from the assembly name:

Localization does not work by default. Localization failed due to the way to search for resources in the assembly. RootNamespace is a build time value that is not available to the running process.

If the RootNamespace is different from AssemblyName, include the following in AssemblyInfo.cs (parameter values ​​are replaced by actual values):

 using System.Reflection; using Microsoft.Extensions.Localization; [assembly: ResourceLocation("Resource Folder Name")] [assembly: RootNamespace("App Root Namespace")] 

More here

0
source

I am using VS 2017 targeting .NET Core 2.1, and in my case the project file (.csproj) contained some strange ItemGroup tags, for example:

 <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.

Earlier, I experimented with adding and deleting resource files, which could cause the appearance of groups of elements, but it is strange that Visual Studio even inserted these lines. It seems like a mistake.

0
source

All Articles