Multiple languages in an ASP.NET MVC application?

asp.net MvcInternationalizationMultilingual

asp.net Mvc Problem Overview


What is the best way to support multiple languages for the interface in an ASP.NET MVC application? I've seen people use resource files for other applications. Is this still the best way?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

If you're using the default view engines, then local resources work in the views. However, if you need to grab resource strings within a controller action, you can't get local resources, and have to use global resources.

This makes sense when you think about it because local resources are local to an aspx page and in the controller, you haven't even selected your view.

Solution 2 - asp.net Mvc

I found this resource to be very helpful

Its a wrapper round the HttpContext.Current.GetGlobalResourceString and HttpContext.Current.GetLocalResourceString that allows you to call the resources like this...

// default global resource
Html.Resource("GlobalResource, ResourceName")

// global resource with optional arguments for formatting
Html.Resource("GlobalResource, ResourceName", "foo", "bar")
   
// default local resource
Html.Resource("ResourceName")

// local resource with optional arguments for formatting
Html.Resource("ResourceName", "foo", "bar")

The only problem I found is that controllers don't have access to local resouce strings.

Solution 3 - asp.net Mvc

Yes resources are still the best way to support multiple languages in the .NET environment. Because they are easy to reference and even easier to add new languages.

Site.resx
Site.en.resx
Site.en-US.resx
Site.fr.resx
etc...

So you are right still use the resource files.

Solution 4 - asp.net Mvc

The Orchard project uses a shortcut method called "T" to do all in-page string translations. So you'll see tags with a @T("A String to Translate").

I intend to look at how this is implemented behind the scenes and potentially use it in future projects. The short name keeps the code cleaner since it will be used a lot.

What I like about this approach is the original string (english, in this case) is still easily visible in the code, and doesnt require a lookup in a resource tool or some other location to decode what the actual string should be here.

See http://orchardproject.net for more info.

Solution 5 - asp.net Mvc

Some of the other solutions mentioned as answer do not work for the released version of MVC (they worked with previous versions of alpha/beta).

Here is a good article describing a way to implement localization that will be strongly-typed and will not break the unit testing of controllers and views: localization guide for MVC v1

Solution 6 - asp.net Mvc

This is another option, and you'll have access to the CurrentUICulture in the controller:

Check MVC3-multi-language

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLance FisherView Question on Stackoverflow
Solution 1 - asp.net MvcHaackedView Answer on Stackoverflow
Solution 2 - asp.net Mvcuser10479View Answer on Stackoverflow
Solution 3 - asp.net MvcNick BerardiView Answer on Stackoverflow
Solution 4 - asp.net MvcBrady MoritzView Answer on Stackoverflow
Solution 5 - asp.net MvcADBView Answer on Stackoverflow
Solution 6 - asp.net MvcdatajohnsonView Answer on Stackoverflow