Mvc: Can't get localization to work by following the guide provided in the docs

Created on 4 Jan 2018  路  5Comments  路  Source: aspnet/Mvc

Im using Microsoft.AspNetCore.All 2.0.3

I've created a new MVC project from Visual Studio.
I've followed the guide here to setup localization: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization

I've added culture specific language files:

  • Resources/Controllers.HomeController.en-US.resx
  • Resources/Controllers.HomeController.sv-SE.resx
  • Resources/Views.Home.Index.en-US.resx
  • Resources/Views.Home.Index.sv-SE.resx

When I run the application, nothing gets localized.

However, if I add a resource file without the culture prefix, strings from the that file becomes "localized".

  • Resources/Views.Home.Index.resx (The value from this file works)
  • Resources/Controllers.HomeController.resx (The value from this file works)

When debugging, I can see that the following SearchedLocation is used: Web.Resources.Controllers.HomeController
Shouldn't this path use cultureinfo or something and look like this: Web.Resources.Controllers.HomeController.en-US?

I've created a small application here that reproduces the behaviour im describing, just clone the repo and run it: https://github.com/joseftw/aspnetcore.localization

What am I missing? I followed a bunch of different tutorials that I've found online, none of them works, they all work when I add files without the culture prefix though...

Steps to reproduce

English

http://localhost:63836/?culture=en-US (or just http://localhost:63836/)

Expected output

Current Culture: en-US
Current UI Culture: en-US
Translated Heading(IStringLocalizer): Heading from resource file!
Translated Heading(IViewLocalizer): Heading from resource file!

Actual output

Current Culture: en-US
Current UI Culture: en-US
Translated Heading(IStringLocalizer): Heading
Translated Heading(IViewLocalizer): Heading

Swedish

http://localhost:63836/?culture=sv-SE

Expected output

Current Culture: sv-SE
Current UI Culture: sv-SE
Translated Heading(IStringLocalizer): Rubrik fr氓n resourcefil!
Translated Heading(IViewLocalizer): Rubrik fr氓n resourcefil!

Actual output

Current Culture: sv-SE
Current UI Culture: sv-SE
Translated Heading(IStringLocalizer): Heading
Translated Heading(IViewLocalizer): Heading

UPDATE: I found this issue on stackoverflow: https://stackoverflow.com/questions/39132868/localization-in-asp-net-core-mvc-not-working-unable-to-locate-resource-file

If I install that package, everything works as it should. But I don't think that's a nice solution since it's a third party package, surely it must be a missing step in the documentation or something?

investigate

Most helpful comment

I had the exact same problem as @joseftw... I tried to fix it for some time and when reading here I finally realized.. "Clean -> Rebuild". I suspected that after reading that it started working again after a reboot.

All 5 comments

@ryanbrandenburg, can you please look into this? Thanks!

I just tried this again by cloning my own repo, and now it works. It's really weird, only thing I've done is that I've rebooted my computer. I guess that you can close this issue if you can't reproduce it... :)

When I run your sample app I get all the expected results. I tried fiddling with a couple things to see if I could get it back into a failure state but no dice. Let us know if you run into this again, otherwise closing this one.

I had the exact same problem as @joseftw... I tried to fix it for some time and when reading here I finally realized.. "Clean -> Rebuild". I suspected that after reading that it started working again after a reboot.

@InfiRandia It works! You saved my day. I spent about 3hours for debugging
..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NTaylorMullen picture NTaylorMullen  路  66Comments

Tratcher picture Tratcher  路  35Comments

skyflyer picture skyflyer  路  40Comments

xaviergxf picture xaviergxf  路  35Comments

dougbu picture dougbu  路  54Comments