Beating localization into submission on ASP.NET 5

While I'm still enthusiastic about ASP.NET 5, there are things that I run into that seem way harder than they should be. Beta software, no docs, I know... I need to keep my expectations in check. Localization is just such a thing. The samples currently in GitHub along side the code lack context, especially relative to what we're all used to.

I'm in the slow process of porting and refactoring POP Forums to the new bits. I've been fortunate that people have contributed a bunch of translations, now totalling six languages. It's the standard resx files embedded in a class library. Using the resources has not been straight forward. Understanding the new and preferred way to do stuff isn't even remotely obvious even with the blog posts around it, which I think make too many assumptions about what one might know. I'm not suggesting any of it is "wrong," just ranting because I couldn't figure it out.

So here's how I got there, putting resx files in a portable class library (PCL) and using them from the web project.

  1. Drop the resx files into your PCL project, including the .designer.cs file. I have this:
  2. Right-click on Resources.resx and click "run custom tool." This will regenerate the designer file, and do it wrong.
  3. Open the designer file. The UI for the resx files won't let me select internal or public, so I did a manual replace all from "internal" to "public." I also had to change the namespace (it made it PopForums.Resources, making the full type name PopForums.Resources.Resources). Then in the static ResourceManager member, you'll have to also change the type name to match.
  4. Open you your project.json file. In order to embed the resx files, you'll need to let the compiler know. Add these two parts:
     "resource": ["Resources/*.resx"],
     "namedResource": { "PopForums.Resources" : "Resources/Resources.resx" }

    The first pair tells the compiler where to look for the files to embed, and the second I'm not sure. I found a half-clear anecdote about using named resources, and I apparently needed this to make it work.

  5. In the web project, I could use the stuff already in my views PopForums.Resources.MarkAllForumsRead for a button label.

I'm sure this isn't right, if only for the fact that adding anything to the resource files will require you to again manually change a bunch of stuff. I'm not even sure if you can use the other languages (I haven't tried). At this point, I'm less interested in the right way and more interested in moving forward with other stuff. I can come back to this, but along with things like configuration, data access and such, localization has to be at least hacked into submission so I can keep working. Hopefully this helps someone out for their own short-term fix.

2 Comments

  • First of all thanks for sharing the good stuff.
    But I hope you're also opening issues in the ASP.NET GitHub Repo, and it would also be a good thing to share a link to them in here so people can subscribe to your issues.
    I believe the ASP.NET team can't fix problems you don't report...

    The lack of strongly-typed Settings file issue you blogged about, also annoys me very much, this, and the lack of attaching external web-services, and the lack of TPT in EF7, had me start a new project in ASP.NET 4.

  • I considered submitting an issue, but the problem is that I'm not sure how to determine if I'm encountering an issue or a feature. Some things are obvious, like if a method called LightUpMyScreen() doesn't do what it says. But in this case, it's not clear by code alone what the intent is.

Add a Comment

As it will appear on the website

Not displayed

Your website