Home > Development > Localizing ASP.NET MVC web application

Localizing ASP.NET MVC web application

January 27, 2010 Leave a comment Go to comments

As I mentioned in last post, using straight forward .NET localization feature will not buy us any benefits in as ASP.NET MVC application, the reasons again are:

  • ASP.NET MVC web applications do not use server control, which will make us lose the advantage of localize controls implicitly.
  • Resources can’t be used outside the web application (I am talking about the Global resources here)

So I did some search and I found some approaches and following is the one I liked the most.

  • Create a new class library project in you solution which will have all resources (Global and/or Local) files needed.
  • Add resource file to the project (i.e. Global.resx) and any other languages versions of this file.
  • Open the properties window for that file (right click the file –> Properties); you will find this by default its Build Action is Embedded Resource.
  • Change Custom Tool property to PublicResXFileCodeGenerator instead of ResXFileCodeGenerator, you can do that by opening the resource file in the editor and change the Access Modifier drop down list to Public.
    LCAL01
  • add your Custom Tool Namespace property to whatever namespace you like to use (i.e. MyResources)
    LCAL00
  • Include a reference for this project and enjoy the strongly typed resources objects (i.e. MyResources.Global.string1).

By having custom namespace you can build your own structure for resources files; (MyResources.Global, MyResources.Views.About, MyResources.ValidationMessages, etc…)

Also it does not break .NET native localization, so you still can add App_GlobalResources and App_LocalResources folder and add resources files and use them as discussed here.

Thanks for this post

Advertisement
  1. kewa
    April 22, 2010 at 10:20 am | #1

    Hi Mina,

    This article is very useful but I’m missing something :
    How do I get resources on pages ?

    For resources in App_GlobalResources, I simply write :

    Thanks

  2. Mina Labib
    April 26, 2010 at 9:50 am | #2

    Hello Kewa, to get the resource in your page you just need to:
    - reference the culture project in your webapplication/website project.
    - retrieve the string by using the key as following:

    Example:

  3. kewa
    April 27, 2010 at 6:54 am | #3

    Sorry but I can’t see your example.
    Apparently the same thing happened to mine…
    Maybe a bug of WordPress.

    Could you resend it ?

    Mina Labib :
    Hello Kewa, to get the resource in your page you just need to:
    - reference the culture project in your webapplication/website project.
    - retrieve the string by using the key as following:
    Example:

    • Mina Labib
      April 27, 2010 at 7:49 am | #4

      Oh yeah, I can see that. here you are the steps to get the resource in your page again:
      - reference the culture project in your webapplication/website project.
      - retrieve the string by using the key as following:
      <%= [Custom tool namespace].[resource file name].[resource string key] %>
      Example:
      <%= MyResources.Global.stringKey %>

  4. kewa
    April 27, 2010 at 7:52 am | #5

    Ok I will test this way instead of :
    <%$ Resources:BMW.GestionSinistres, damage_declaration %>

    @WordPress : please notice that ASPX isn’t html ^^

  1. January 27, 2010 at 1:48 pm | #1

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.