Use ASP.NET MVC T4 custom templates to generate resource files and localized views.
Table of contents
- Overriding ASP.NET MVC custom templates
- Understanding .resx file format
- Creating resource files
- Creating localized views
- References
- Downloads
Overriding ASP.NET MVC custom templates
The Add View dialog perform code generation that use 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.
You can find more information here:
T4 Templates: A Quick-Start Guide for ASP.NET MVC Developers
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:
You have the corresponding XML:
I created a custom template that generates the resource file in the specified format.