views:

62

answers:

2

Hopefully this isn't too silly of a question. In MVC there appears to be plenty of localization support in the views. Once I get to the controller, however, it becomes murky.

  • Using meta:resourcekey="blah" is out, same with <%$ Resources:PageTitle.Text%>.
  • ASP.NET MVC - Localization Helpers -- suggested extensions for the Html helper classes like Resource(this Controller controller, string expression, params object[] args). Similarly, Localize your MVC with ease suggested a slightly different extension like Localize(this System.Web.UI.UserControl control, string resourceKey, params object[] args)

None of these approaches works while in a controller. I put together the below function and I'm using the controllers full class name as my VirtualPath. But I'm new to MVC and assume there's a better way.

    public static string Localize (System.Type theType, string resourceKey, params object[] args) {
         string resource = (HttpContext.GetLocalResourceObject(theType.FullName, resourceKey) ?? string.Empty).ToString();
         return mergeTokens(resource, args);
    }

Thoughts? Comments?

A: 

I use strongly typed resources using PublicResXFileCodeGenerator as Custom Tool (selected in properties of .resx file). If I have resource with 'AreYouSure' name and 'Are you sure?' value, it can be accessed by calling ResourceClass.AreYouSure, both in controller and views. It works pretty well.

LukLed
I'd be glad to hear otherwise, but that requires resource files embedded in the assembly (which auto generate the classes your relying on). My resources are stored in the database so this solution won't work.
EBarr
A: 

What do you want to localize in your controller? Normally only what is shown to the user should be localized. And what is shown to the user should be inside a view so back to the helpers.

Darin Dimitrov
Validation messages for example. Maybe not in controller, but in service layer.
LukLed
EBarr
Yep, the sample MS projects do not consider the non english speakers as users :) Anyway, why instead of setting a message, you don't just set a code to use later for looking up the correct message?
uvita