ASP.NET MVC 3 Internationalization
I have been doing some work with internationalization (globalization, localization, whatever you want to call it, and yes I do know they all mean something slightly different) in ASP.NET MVC 3 and thought I would share a couple of tips.
The Past
Internationalization has always been a bit of a pain in the #youknowwhat with ASP.NET MVC up to now. For me it’s always been like, “it’s so close I can smell it, but it’s not quite there yet”. In the past I have created custom HTML Helpers to let me do something like this in the View:
<%: Html.LabelFor(model => model.Surname, PersonResources.Surname ) %>
This custom helper allowed me to describe the the property that I wish to associate the label to and the text I wish to display. It turns out that a lot of people must have requested this as this functionality is now provided out-of-the-box in ASP.NET MVC 3.
I have also heard of people doing all sorts of workarounds, like creating localized copies of each View (surely a maintenance nightmare!), using JavaScript to look up localized versions of UI text in XML files, etc., etc. ….
But there is now a better way!
The problem actually all stems from the DisplayNameAttribute, which you can use to decorate the properties in a model with a user-friendly name which is displayed in the UI by default instead of the actual name of the property. For example, if you had a property called ContactEmail and you decorated it with [DisplayName(”Email Address”)], whenever you referred to the ContactEmail property in the UI (maybe using an HTML Helper) the string “Email Address” would be displayed in place of the property name (ContactEmail).
The problem is DisplayNameAttribute has only one constructor (barring the empty one):
DisplayNameAttribute(string displayName)
Since there was no way to pass in a reference to a resource file we could only pass hard-coded strings to the UI – no good for a UI that is supposed to be multi-language.
The Solution
This problem has been resolved with ASP.NET MVC 3 which, because it is built on .NET Framework 4, has access to the DisplayAttribute class.
This class has several properties, including the Name and ResourceType properties to look at a particular resource file and name/value pair within it. For example:
[Display(Name="Surname", ResourceType=typeof(PersonResources))]
public string Surname { get; set; }
This will use the relevant resource file (in this instance called PersonResources.resx), according to the current language, to display the localized value for Surname wherever it is referred to in the UI.
@Html.LabelFor(model => model.Surname)
Summary
I really hope this makes sense. Internationalization is a job that developers often try to avoid, but these improvements in ASP.NET MVC 3 make, at least this bit of the job, much, much easier.
Leave any questions you may have in the comments and I will do my best to answer them for you.













DotNetShoutout 4:20 pm on January 23, 2011 Permalink
ASP.NET MVC 3 Internationalization « .Net Web stuff, mostly….
Thank you for submitting this cool story – Trackback from DotNetShoutout…
D Clumsy 1:48 pm on January 26, 2011 Permalink
I usually subclass the attribute to use translations which are stored in a database rather than a .resx text file.
Ricardo 4:42 pm on May 17, 2011 Permalink
Very nice, thank you for sharing. How do you go about getting translated strings from a database? will this approach still work and how does differ from using resource files? Thanks!
Nobels 9:20 pm on November 23, 2011 Permalink
Hello,
Your example works indeed. But when you forget 1 word in your resourece you get an error.
Is there a way to catch that problem, and returns the key as value?
i would like to here an anwser to my question.
Thanks