MVC internationalization/localization weirdness

I’ve just encountered a weird decision in the ASP.Net MVC framework that I don’t understand.  When it comes to interpreting parameters especially complex ones being done via ModelBinding it will assign the parameters different cultures depending on where the parameters came from.  I’m kind of wondering why that’s the case.

The specific reason I now know this, and have fudged my code to get around that is that I’m passing dates on the query string using a form with a GET.  Since I’m in little england my date got read the wrong way and I kept getting my month and day reversed. Previously the code I’d written that was attached to a form that did a POST worked fine, and continued to while I was pulling my hair out wondering why my new code was broken.

The part that appears to be responsible for this is the ValueProviderDictionary and there we find this comment.

// We use this order of precedence to populate the dictionary:
// 1. Request form submission (should be culture-aware)
// 2. Values from the RouteData (could be from the typed-in URL or from the route's default values)
// 3. URI query string

As I’ve implemented my own date model binder like Scott Hanselman I’ve put this fudge into it,

// KLUDGE: force the culture since for some reason the MVC platform only uses the
// users culture if the results come from a form, not if they are from a route or query string.
if (valueResult.Culture == CultureInfo.InvariantCulture)
{
    valueResult = new ValueProviderResult(valueResult.RawValue, valueResult.AttemptedValue,
                                          System.Threading.Thread.CurrentThread.CurrentUICulture);
}
return (T?)valueResult.ConvertTo(typeof(T));

It’s nuts but it works around my problem.  There may be a better way to hook into the framework but since I don’t really understand their reasoning for doing that I’ve decided to leave it alone for now.

About these ads

3 thoughts on “MVC internationalization/localization weirdness

  1. [...] 7, 2009 at 9:18 pm · Filed under Uncategorized Shortly after writing that post on the ‘weird’ MVC decision I just figured out the reasoning behind the invariant culture for the other types of [...]

  2. [...] 11:40 pm · Filed under Programming ·Tagged mvc I’ve now written a solution to my date localization problem in MVC 1.0.  It looks like MVC 2.0 will require a different implementation because they’ve [...]

  3. [...] to VoteMVC internationalization/localization weirdness (12/7/2009)Monday, December 07, 2009 from colinnewellI’ve just encountered a weird decision in the ASP.Net [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 64 other followers