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

genericuser picture genericuser · Aug 24, 2016 · Viewed 16k times · Source

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?

Answer

user2389913 picture user2389913 · Apr 9, 2017

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.