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
- You put the
App_LocalResources
folder as a sub folder within your respective View folder
- You create resx files that correspond to your Views... IE:
Index.resx
or About.resx
- You set the Resx files properties to be "Public" (PublicResXFileCodeGenerator), "Embeded Resource", "Views.[viewname]" namespace
- Create a new resource name called "Title" and put whatever you want in the Value.
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…