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.
- Drop the resx files into your PCL project, including the .designer.cs file. I have this:
- Right-click on Resources.resx and click "run custom tool." This will regenerate the designer file, and do it wrong.
- 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.
- 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.
- 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.