2

I'm looking to use App_LocalResources in my MVC Application, but I'm wondering about a solid implementation.

My idea is to loop through the Resx file and grab all of the Keys. The store those keys in an object from the ViewModel.

Then in my View, I can use <%: model.resources.key %> to extract the information.

Do any of you guys know how I can efficiently extract all of the values of a Resource file and dump them into an Object in the ViewModel? Is this even possible?

EDIT:

I have discovered how to get a single record out of the Resx file, just not the whole thing.

        Dim whats_next = HttpContext.GetLocalResourceObject("~/views/about/faq.aspx", "whats_next").ToString
        ViewData("whats_next") = whats_next
        Return View()

What I need to be able to do is this (where the ViewModel.Resources Object is just a List(Of KeyValuePairs))

        Dim ViewModel As Domain.FAQViewModel = New Domain.FAQViewModel()
        ViewModel.Resources = ''# enumerate APPROPRIATE LOCAL resource file and store all keys
        Return View(ViewModel)

From here, I can extract all of the keys in my view

        <%: model.Resources.ResourceKey1 %><br />
        <%: model.Resources.ResourceKey2 %>
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Note, I did have this previous question open, but the answers were incorrect, and my question is unclear. I'm now approaching it from a new angle. Basically I absolutely do NOT want `runat="server"` anywhere in my Views. http://stackoverflow.com/questions/2970935/how-does-the-app-localresources-work-with-mvc – Chase Florell Nov 03 '10 at 21:06
  • unless i'm missing something, this doesn't sound like the correct approach. why would you need to *grab all the keys*? Are you saying you want to say populate 5 different translations for 1 word in the ViewModel? I would be more inclined to look at the culture info, then put the relevant string into the viewmodel. What looping is needed? – RPM1984 Nov 03 '10 at 21:08
  • just saw your edit - why are you wanting to display ALL the resource files in a view? – RPM1984 Nov 03 '10 at 21:09
  • I want to be able to access them in the view from a single object. `model.resources.resourcekey` – Chase Florell Nov 03 '10 at 21:29
  • I don't want all of the locals... just the relevant one. `If user is French, Grab the FAQ resource in French, and store ALL keys for FAQ.aspx, Then in the view, spit out the appropriate value depending on the Key requested` – Chase Florell Nov 03 '10 at 21:51

1 Answers1

0

Any particular reason that you want to use App_LocalResources instead of App_GlobalResources or even a regular resource file? If you had a regular resx file you could access it like this:

<%: Resources.ResourceClass.MyResourceString %>

App_LocalResources are not really supported in MVC views, plus it seems inefficient (at least without caching) to enumerate all resources if you are only going to use 1 or 2 in your view.

It's probably easies if you just create a number of "regular" resource files (i.e. not ones in App_GlobalResources). You can have as many as you like, for example one per each view.

marcind
  • 52,944
  • 13
  • 125
  • 111
  • well that's just it. I create a View Specific resource file and enumerate all records that are just for that view. Then I don't need to maintain a single GlobalResource file that could get out of hand when I have 20 or 30 views, each with 20 or 30 keys. – Chase Florell Nov 03 '10 at 21:28
  • You can have as many 'regular' resource files as you like (since this is a WAP). – marcind Nov 03 '10 at 21:34
  • I can do this approach, it just doesn't seem as clean. – Chase Florell Nov 03 '10 at 21:50
  • I'm curious why you see this approach as not clean? Is it because the resource string is not explicitly retrieved from the model? When you think about it, the model usually represents your data and state. It doesn't (or at least it shouldn't, in my opinion) represent the hardcoded UI elements of a page. Let's assume that your application only supports one language (does not require localized strings) - would you place all of the UI strings on the model? – marcind Nov 03 '10 at 22:25
  • I guess it doesn't seem clean to me simply because you aren't separating the resources files the same way you would with "local" – Chase Florell Nov 03 '10 at 22:31
  • You still can have as many resource files as you'd like. For example: http://adamyan.blogspot.com/2010/02/aspnet-mvc-2-localization-complete.html. The only difference being that there's no restriction on a given resource being associated and accessible from only one .aspx. – marcind Nov 03 '10 at 23:19