0
votes

I have an ASP .NET 5 RC1 website to which I am trying to add localization. Based on the information found I did the following

  • In the ConfigureService in Startup.cs:

    • Enable Localization and setting the ResourcePath to "Resources"
    • Enable View Localization and Enable Data Annotations Localization

      //check http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
      services.AddLocalization(options => options.ResourcesPath = "Resources");
      // Add MVC services to the services container.
      //check http://blogs.msdn.com/b/webdev/archive/2015/10/15/announcing-availability-of-asp-net-5-beta8.aspx
      services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization();
      
  • In the Configure method fin Startup.cs

    • Setup the list of supported cultures
    • Enable Request Localization

      //check http://www.jerriepelser.com/blog/setting-thread-culture-aspnet5
      //check http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
      List<CultureInfo> supportedCultures = new List<CultureInfo>()
      {
          new CultureInfo("en"),
          new CultureInfo("es")
      };
      var requestLocalizationOptions = new RequestLocalizationOptions()
      {
          SupportedCultures = supportedCultures,
          SupportedUICultures = supportedCultures
      };
      app.UseRequestLocalization(requestLocalizationOptions, new RequestCulture(new CultureInfo("es")));
      
    • Create a Resources folder under the project

  • Create the Resources for the Controller. With the convention {Project}.{Controllers}.{ControllerClassName}.{culture}.resx
  • Create the Resources for the Views. With the convention Views.{ViewFolder}.{ViewName}.cshtml.{culture}.resx
  • Use the IHtmlLocalizer in the controller, and access the item. In this case localizer["Title"], which is found and works just fine. However when the culture is set to "es" it is not found and just falls back to the default resource.

    private IHtmlLocalizer<HomeController> _htmlLocalizer;
    public HomeController(IOptions<PTIWebPortal.Configuration.PTIWebPortalConfiguration> pConfiguration, 
        ILoggerFactory factory, IHtmlLocalizer<HomeController> localizer) : base(pConfiguration, factory)
    {
        this._htmlLocalizer = localizer;
    }
    

The same is happens for the views, it only works with the default resource, but not with the others.

Any ideas on how to fix it?

1
Are you setting the Thread.CurrentCulture or Thread.CurrentUICulture? Also, what is the value of the CultureInfo.CurrentCulture.Name property when it fails to load? - Ross Bush
That's the weird part, the Thread culture properties are correct, and being set automatically, yet the non default resource is not being found for whatever reason - Eduardo Fonseca
Does the resource file for the locale have the Copy To Output Folder property set to "Copy always"? - Ross Bush
I don't think ASP .NET 5 projects have Copy To Output anymore, the option is not shown in any of my files under the project. - Eduardo Fonseca
If you right-click the .resx file and select properties it should have this property. BuildAction->"Embeded Resource". Copy To Output->"Always" - Ross Bush

1 Answers

0
votes

There are a lot of known issues of things not working for localization as of rc1. Some of the known issues are tooling related, so some of the localization things work if you run the app from the command line instead of launching from visual studio. But even from the command line a lot of things that were supposed to work do not work. There has been quite a bit of work after rc1 and a lot of localization issues have been fixed and closed recently, so things should be much better after rc2 is released sometime in February.