Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 470
genericuser Avatar asked Aug 24 '16 20:08

genericuser


People also ask

What is localized application in asp net?

Localization, on the other hand, is the process of customization to make our application behave as per the current culture and locale. These two things go together. To do globalization we need to use Resource Files.


1 Answers

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.

like image 171
user2389913 Avatar answered Sep 22 '22 13:09

user2389913