70

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?

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
  • possible duplicate of [How to localize ASP.NET MVC application?](http://stackoverflow.com/questions/192465/how-to-localize-asp-net-mvc-application) – Marijn Feb 08 '12 at 11:09
  • multi language website with out passing parameter, check this [Multi Language Website In MVC 4 C#](http://lesson8.blogspot.in/2013/03/multi-language-website-in-mvc-4-c.html) – Sender Mar 27 '13 at 06:07

6 Answers6

43

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.

Haacked
  • 58,045
  • 14
  • 90
  • 114
  • Yes this does make sense for controllers. But what about View Models? They are (often) specific to the view and it would make sense to reference view specific resources in the view model code. I wonder if there is a mechanism to make a resource file local to a view and its view model... – Andy McCluggage Dec 21 '10 at 10:35
  • @Haacked, hi, we have a shopping website in asp mvc , so when user selects arabic language, we want to make the whole site arabic , can we use microsoft bing [translator](https://www.bing.com/translator/) or microsoft [translator API](http://www.microsoft.com/en-us/translator/products.aspx) or should i buy [localize.js](https://localizejs.com/features) to achieve this fast ? – Shaiju T Oct 06 '15 at 22:42
22

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.

buræquete
  • 14,226
  • 4
  • 44
  • 89
user10479
  • 790
  • 2
  • 9
  • 22
3

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.

Nick Berardi
  • 54,393
  • 15
  • 113
  • 135
2

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.

Brady Moritz
  • 8,624
  • 8
  • 66
  • 100
  • There are cases where the exactly identical source string "A String to Translate" would have to be translated differently based on the surrounding context. This especially happens with single-word strings. – springy76 Feb 16 '12 at 09:33
1

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

ADB
  • 2,319
  • 1
  • 24
  • 35
0

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

Check MVC3-multi-language

Rahul Patel
  • 5,858
  • 6
  • 46
  • 72