Click here to Skip to main content
14,272,143 members

ASP.NET MVC Localization: Generate resource files and localized views using custom templates

Rate this:
5.00 (5 votes)
Please Sign up or sign in to vote.
5.00 (5 votes)
2 Aug 2011CPOL
Use ASP.NET MVC T4 custom templates to generate resource files and localized views

Table of Contents

Overriding ASP.NET MVC Custom Templates

The Add View dialog performs code generation that uses T4 templates behind the scenes. These templates can be modified to customize the generated code from these tools. You can also add custom templates.

Basically you have to copy the default templates to your project. This is the path:

[Visual Studio Install Directory]\Common7\IDE\ItemTemplates\
[CSharp | VisualBasic]\Web\MVC\CodeTemplates\

I have created a custom template that generates resource files (.resx), and I have modified the default templates to use the generated resource files.

Views Templates

You can find more information here:

Understanding .resx File Format

From MSDN [1]:

The .resx resource file format consists of XML entries, which specify objects and strings inside XML tags. One advantage of a .resx file is that when opened with a text editor (such as Notepad or Microsoft Word) it can be written to, parsed, and manipulated.(…) Each entry is described as a name/value pair.(…) When a string is added to a .resx file, the name of the string is embedded in a <data> tag, and the value is enclosed in a <value> tag.

For a resource file like this:

Image 2

You have the corresponding XML:

Image 3

I created a custom template that generates the resource file in the specified format.

Image 4

Creating Resource Files

Add a strongly-typed view using the custom template and the resource file will be generated for you:

Image 5

This is the generated file:

Image 6

It’s necessary to edit the generated text. I decided to surround some properties with [[propertie-name]] – this way is easy to replace it using Find-Replace inside a text editor. Don’t forget to set the Access Modifier property to Public.

Image 7

Set the following properties of the resource file:

Image 8

Rebuild the Web application and you’re ready to use the generated resource.

Creating Localized Views

Add a strongly-typed view:

Image 9

This is the generated code. Set the namespace to the same value you used in the resource file properties:

Image 10

That’s it! The view will reference all the values that were created in the resource file.

English language screenshot

Image 11

Italian language screenshot

Image 12

Portuguese language screenshot

Image 13



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rui Jarimba
Software Developer (Senior)
Italy Italy
My name is Rui Jarimba and I was born in Madeira island, Portugal and I currently live in Rome, Italy.

I have more than 10 years of experience developing software using the .NET Framework and other technologies (Web development, Databases, ...).

Some of my professional interests are: software development best practices, software architecture, cloud computing, Continuous Integration (CI), Continuous Delivery (CD) and agile methodologies such as Scrum, Kanban, Lean and any other methodology that can help me to become a better and more productive software engineer.

I believe in good code - code that is readable, maintainable, reusable, testable and deployable. This means that I'm not the "quick and dirty" type, I write code for the medium/long term whenever possible.

Something else about me - I love music, I am an amateur photographer, not a big fan of gyms (I prefer to do some outdoor activity such as walking/hiking), big foodie (I love Mediterranean cuisine and my glass of wine!).

Comments and Discussions

GeneralMy vote of 5 Pin
Helluin5-Mar-12 19:27
memberHelluin5-Mar-12 19:27 
GeneralRe: My vote of 5 Pin
Rui Jarimba18-Jul-12 8:30
professionalRui Jarimba18-Jul-12 8:30 
QuestionLocalize the database Pin
Member 862530014-Feb-12 8:33
memberMember 862530014-Feb-12 8:33 
AnswerRe: Localize the database Pin
Rui Jarimba14-Feb-12 9:55
professionalRui Jarimba14-Feb-12 9:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 2 Aug 2011

Tagged as


15 bookmarked