Windows Phone 7 Developer Launch - Learn More
kick it on DotNetKicks.com   Shout it  

ASP.NET MVC Globalization/Localization: Referencing Resource Files in Your View

I've been using ASP.NET MVC since Preview 1 (Nov 2007). When I need to reference a Resource File (.resx) in my View, I just do the following:

   1: <%=Resources.MyResourceFile.MyResourceKey%>

Until today, I took that for granted. I received an email this morning referencing this post on ASP.NET. The post was about using Resource Files in an MVC View. I replied to the original post in February 2008 and then forgot about it. Today, I was asked how I got this to work. Since I hadn't ever given it much thought – it just worked – I dove in a bit to answer the post and here's what I got.

Create Your Resource (.resx) File

There are 3 ways to create a Resources file in an ASP.NET application (MVC or WebForms):

  1. Add New Item: Right-click anyware –> Add –> New Item…
  2. Add to App_GlobalResources:
    1. Right-click your project –> Add –> Add ASP.NET Folder –> App_GlobalResources
    2. Right-click App_GlobalResources –> Add –> New Item…
  3. Add to App_LocalResources:
    1. Right-click a folder –> Add –> Add ASP.NET Folder –> App_LocalResources

Local Resource Files

1 and 3 are essentially the same, I'll refer to the result as a local resource file.When you create a local resource file all you need to do after you've added a view string values to the file is change the “Access Modifier” setting to “Public”.

VSResourceFile

The only difference (as far as I can tell) between 1 and 3 is that the default value for Access Modifier for 1 is “Internal” and for 3 it's “No code generation”. There may be some differences with regard to WebForms, but honestly I never noticed before.

Global Resource Files

Number 2 is just as easy, but there is a little more to explain, and here's why: you can't change the Access Modifier for a Global Resource File. If you create a Global Resource File and open it you'll notice the option is disabled.

VSGlobalResourceFile

The only option for a Global Resource File is “Internal”. It reads public on the toolbar. But if you look at the designer.cs file you will see that the class and all its properties are marked as “internal”.

However, you can still easily use a Global Resource File, with just one caveat. Intellisense doesn't quite work the way you would expect (or maybe you would). The namespace will show up on intellisense, the class name will not, but once you have correctly typed the namespace and class name you'll get intellisense for all your keys.

Here you see the namespace in intellisense:

VSIntellisenseNamespace

Here you see nothing comes up for the class:

VSIntellisenseClass

But here you see the keys are picked up by intellisense once you enter the class name correctly:

VSIntellisenseProperties

The funny thing is that this is not always true. Sometimes you will get intellisense for the class name and sometimes you won't. Either way you can either use Local Resource Files or as suggested by Steven Sanderson in his book Pro ASP.NET MVC Framework (Apress) you can create a separate Class Library Project for resources and just make the visibility public.

I've seen extension functions written for access to resources and frankly this seems the simplest way. Not to mention you get compile checking and intellisense support. I really don't like having to type in string keys, I make too many typing mistakes to trust myself in that way.

Tags: ,

kick it on DotNetKicks.com   Shout it  

Feedback

# 

Gravatar This is because of the Custom Tool that is set when you add the file, and can be changed. If you click on the resource file and open the properties window, change the "Custom Tool" to ResXFileCodeGenerator and the "Custom Tool Namespace" to Resources, the "Access Modifier" dropdown will be enabled with all options. 7/6/2009 2:04 AM | noreply@blogger.com (merc)

# re: ASP.NET MVC Globalization/Localization: Referencing Resource Files in Your View

Gravatar Never noticed this post before. I don't know if you would still do this now, but I can't imagine creating a View without a view-specific model now, so I wouldn't do what you suggest here. Since I would be using a model class to abstract things, I wouldn't have to battle with spotty Intellisense coverage or the other issues regarding public or internal resource files.

I just used resource files for the purposes of i18n on my last project and the way I did things was this: I have an interface called ILanguageProvider that essentially is nothing more than a list of properties, e.g. like "FirstNameLabel" and "EmailHasBeenSentMessage", etc. Then the classes that implement that interface are named "KoreanLanguageProvider" and "EnglishLanguageProvider", etc. Each of those then wrap calls to language-specific resource files to do the dirty work.

Lastly, I have a static method that is called like this:

ILanguageProvider languageProvider = LanguageProviderFactory.GetLanguageProvider(Language.English);

Basically, that instantiation happens within a view model class, setting a property of type ILanguageProvider to expose it to the View. And, of course, the language is not static like it is in my example, but is dynamically determined based on either the user's selection when they sign in or based on their preference as stored in the database, etc. etc.

Nothing too exciting or groundbreaking by any stretch of the imagination, but it was nice and worked well enough. 11/5/2009 12:16 AM | Jason Bunting

# re: ASP.NET MVC Globalization/Localization: Referencing Resource Files in Your View

Gravatar Thanks for great guidelines. 1/4/2010 11:54 AM | Web design company

# re: ASP.NET MVC Globalization/Localization: Referencing Resource Files in Your View

Gravatar Hi Mark,

Thanks for the article! Do you know of any way to use resources files but not be restricted by language? I have MyApp.resx, MyApp-fr-FR.resx, but I want another english version, let's say:

MyApp.[whatever].resx

So essentially based on some application settings I want different language for the same variables but not want it language based but application based.

One application might reference contacts as "subscribers" and another as "leads".

Thanks for your help. 2/12/2010 6:13 PM | Kevin

# re: ASP.NET MVC Globalization/Localization: Referencing Resource Files in Your View

Gravatar hi to all .this is kranthi kumar. i have to implement multilingual website in MVC architecture. so that i have gathered all resource files and i attached in my root directory in associate folders.so now i want access all those resource files in to .aspx files. unfortunately we dont have server side controls in MVC.so how we can display with HTML helper classes.
one more thing is that i have to display data dynamically from database based on the current UIculture..so anybody help me for this.i appreciate in advance. 3/5/2010 11:13 PM | kranthi kumar mathi

# disqus.com

Gravatar ASP.NET MVC Globalization/Localization: Referencing Resource Files in Your View 9/9/2014 4:09 PM | disqus.com

Comments have been closed on this topic.
 

 

Copyright © Mark J. Miller