I've been working through my first MVC application, and I'd like to localize it. I know how to use the App_GlobalResources but I'd prefer to use the App_LocalResources.
In a typical WebForms application, you put the App_LocalResources folder in the same directory as the ASPX file... is this the same in MVC (IE: do I put them in the respective subfolders in the "views" directory)?
Also, when accessing the App_GlobalResources in your view you can do it like so <%=Html.Encode(Resources.Index.Title)%>
however I cannot do that with the App_LocalResources. I don't want to have to make my <label runat="server"
in order to take advantage of the meta:resourcekey
EDIT:
I discovered ONE method to do this, but I'm not sure if this is the best method. Here's the idea
App_LocalResources
folder as a sub folder within your respective View folderIndex.resx
or About.resx
Now I can reference the Local Resources using something like this
<%= MyProj.Views.Home.Index.Title%>
The only problem I see with this is the fact that the resources are embedded and I cannot add new locales without re-compiling the project.
Here is my reference on this method.
You should not use App_GlobalResources or App_LocalResources with MVC. That is because of the way these resources work. These resources are not embedded in your project, but are instead compiled by the ASP.NET runtime. This means that your resources will not be available when not running inside the ASP.NET runtime, which means you would lose the ability to unit test your views and controllers if you use resources in them.
This post gives more details about it.
Instead, you should use the approach you mention yourself in your question. Put your resource whereever you want, and set it to 'Embedded resource' and the custom tool should be 'PublicResXFileCodeGenerator'.
I know this doesn't directly answer the question but what I think you are really looking for is "How do I do localization with ASP.NET MVC".
You might check out the answers to this stackoverflow.com question and see if they help answer the problem you want to solve: how-to-localize-asp-net-mvc-application
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With