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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
EBarr
  • 11,826
  • 7
  • 63
  • 85
  • I think that localization is an aspect that should not make part of the controller logic. As this is a presentation-specific aspect, I think that whatever approach you choose, you should make your Views responsible of solving this. I know this doesn't answer your question, but at least is a hint for you to make a decision. – uvita Mar 17 '10 at 00:22
  • @uvita please see my response to @LukLed. I also wanted to add that making localization an issue for the view is a reasonable architecture decision, although this type of strategy forces me to gather up a list of keys/tokens in my controller or validation service layer so they can be passed in my view through ViewData. Also, as far as I can see this means my view would then have to populate the ModelState errors or forgo the Html.ValidationMessage() infrastructure. Wouldn't this be more disruptive to responsibilities of a view than localizing in the service layer? – EBarr Mar 17 '10 at 13:13

3 Answers3

3

I had the same question. This blogpost showed different ways to solve the problem: http://carrarini.blogspot.com/2010/08/localize-aspnet-mvc-2-dataannotations.html

In the end I used a T4 template to generate a resources class. I also have a HtmlHelper method to access my resources:

public static string TextFor(this HtmlHelper html, string resourceName, string globalResourceName, params object [] args)
{
    object text = HttpContext.GetGlobalResourceObject(globalResourceName, resourceName);
    return text != null ? string.Format(text.ToString(), args) : resourceName;
}

Another version generates a localized version from Controller and View:

public static string LocalTextFor(this HtmlHelper html, string resourceName, params object [] args)
{
    string localResourceName = string.Format("{0}.{1}", html.ViewContext.RouteData.Values["controller"],
                                                 html.ViewContext.RouteData.Values["action"]);
    object text = HttpContext.GetGlobalResourceObject(localResourceName, resourceName);
    return text != null ? string.Format(text.ToString(), args) : langName;
}
Community
  • 1
  • 1
slfan
  • 8,950
  • 115
  • 65
  • 78
  • This is similar to what I ended up implementing (last year!). But you get the answer vote for being the first to write it out:-) – EBarr Apr 07 '11 at 19:08
0

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
  • 31,452
  • 17
  • 82
  • 107
  • 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 Mar 16 '10 at 23:09
0

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Validation messages for example. Maybe not in controller, but in service layer. – LukLed Mar 17 '10 at 09:31
  • Specifically, I'm looking at the MVC Starter Kit & the default project created by MVC 2. In both there are a series of username/password validation statements & error messages hard coded in the controller. I'm looking for a proper way to localize these types of statements. I agree (with @LukLed and @uvita) that in larger project a service layer is the more appropriate location for validation. However, I didn't want to complicate the description of my issue. Nevertheless, the answer would be equally applicable to both a controller and a service layer. – EBarr Mar 17 '10 at 13:08
  • 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 Mar 18 '10 at 00:38