ASP.NET - MVC 3: Localization

16,255

Solution 1

Then I found a post that warned about using the App_GlobalResouces and App_LocalResources. (Link)

I found that post extremely useful. The method explained there is very clean. Here is a snippet of my newly created index view using that method:

@using Resources.Index
@{
    ViewBag.Title = "Index";
}

<h1>@Index.Title</h1>

I don't think there is a definitive do-it-this-way-or-else method, so going for the cleanest method seems to me like a good deal.

Solution 2

I've come up with an easier way to handle localization where you don't have to use resource files or attributes. It involves custom metadata providers.

MVC3 uses metadata providers to get all text strings. My approach allows you to either use string tables, a database, flat files or any other source to provide the translations. All you need to do is to inherit two interfaces.

Read about it here: http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/

Edit

Everything is now documented at github and there are nuget packages: https://github.com/jgauffin/griffin.mvccontrib

Share:
16,255
Esa
Author by

Esa

Updated on June 04, 2022

Comments

  • Esa
    Esa almost 2 years

    I am about to implement localization for my MVC3 web application. Googling my way through large amounts of ways to do it, I was left unsure which way to implement this. I found few ways how to do it:

    First option I found was to use App_GlobalResources and ViewData and culture select with Map Routing. (Link)

    Second was to make a separate Resources folder in my project and structure it like Model and View folders. Then create the resource files in those folders. To use those resource strings would be like using the Viewbag. Then edit the Global.asax to handle culture changing and create a partial view to allow changing cultures. The instructions to do this are very thorough so this might be the best bet. (Link)

    Third was to use DisplayAttribute and resx-files. This one was a bit hazy, I could not find the sort of information so that I could grasp how this actually is implemented and its restrictions. (Link)

    Fourth was to create a custom class to handle resources. This seemed pretty fancy and easy to implement and use but no real information was found if it actually worked. (Link)

    Then I found a post that warned about using the App_GlobalResouces and App_LocalResources. (Link)

    After going through possible ways of doing localization. I could not find a way which was universally approved or accepted. This left me pondering, which of these, or some I didn't find, would be the best way to implement localization in ASP.NET MVC3?

  • Esa
    Esa almost 13 years
    I used this one myself too. It was easy to implement and did not require any javascript.
  • Esa
    Esa almost 12 years
    I'm trying your solution at the moment and so far I'm enjoying it, but is it possible to do View translations with the resource file method? Your tutorial at CodeProject only shows this on SQL.
  • jgauffin
    jgauffin almost 12 years
    Yes, but I don't see the point with it. You have to use string keys which are a hash of the text (which makes it hard to enter manage them and/or tell which text each translation is for). It's probably easier to use the string tables directly (or use the flat files built into Griffin.MvcContrib)
  • Kev
    Kev over 11 years
    Would be good if you could note down the the salient points of your blog post here. One day it might not be available, and you've probably been around long enough now to know why we don't like link only answers. Thanks.
  • jgauffin
    jgauffin over 11 years
    @Kev: Well. I said custom metadata providers. That describes the approach pretty well. I added more info though. Github has all the documentation too, so it's not really dependent on my blog post (although it elaborates on everything).
  • Cristian Boariu
    Cristian Boariu about 11 years
    @jgauffin Thanks for your project... Is there any way you keep mantaining this? For instance: http://griffinframework.net/docs/mvccontrib/ is no longer working. Also, I am not able to find anywhere the url to access admin side of your project after installing admin nuget package.
  • jgauffin
    jgauffin about 11 years
    I've moved everything to azure but some web sites are not properly moved yet. The project is not dead. I'm just working on an alternative localization technique which will be published as soon as my Localization book is finished.
  • Pierluc SS
    Pierluc SS over 9 years
    @jgauffin documentation is not currently available
  • jgauffin
    jgauffin over 9 years
    @CristianBoariu: I'll put it back up today