views:

56

answers:

2

I have a feeling this is something very obvious that I'm missing, but here goes.

I'm trying to set up multiple languages in an ASP.Net MVC project (C#, MVC2, .Net4.0). However, I have some cases where I want to have phrases translated within the Controller (error/success messages, mostly). However, it seems every method I try is either protected or internal. How do I access the resx files from within my Controller?

+1  A: 

Make sure the modifier on the resx class that's created for you is Public. When you're editing the resx file, you'll see a drop down with the modifier towards the top right (close to the vertical middle of your screen).

However, consider the fact that you could have your ViewModel take care of localizing validation messages by decorating the different properties with DataAnnotations and resource files.

Esteban Araya
I took a look into the DataAnnotations and that, and didn't get too far. It seems like you need to be using Linq-To-SQL? I'm using NHibernate. I was following this (http://msdn.microsoft.com/en-us/library/cc488527.aspx) tutorial, but my partial class was unable to find my NHibernate class in a different project.
Matt Grande
+2  A: 

This is a presentation specific concern, I think the best approach is to handle this at a View level. You will have less problems if you access resources from the View. Also note that accessing resources from the controllers make them less testable, because when accessing a resource (by default) the resource manager looks for a specific file which is not found when you´re testing.

uvita
I agree. You could include several different possible messages for the user in a view-specific resource and have the controller pass a key or some other kind of indicator that allows the view to decide which message needs to be displayed. I don't think the controller should do any more than that, as how the user is informed of this situation is a decision for the view.
Dr. Wily's Apprentice