Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple languages in an ASP.NET MVC application?

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?

like image 930
Lance Fisher Avatar asked Aug 06 '08 21:08

Lance Fisher


People also ask

Does .NET support multiple languages?

NET framework is a Top-Level Domain (TLD) which was initially developed for networking technology, but now supports multiple programming languages such as Visual Basic, C#, and J# at an intermediate level by interoperating with them.

Which language is used in MVC?

The MVC pattern is widely used in program development with programming languages such as Java, Smalltalk, C, and C++.

How do I change the language of a resource file in ASP.NET MVC?

One way you can do it is to have the drop down just redirect the page to a language specific URL (this is quite nice as you can send around language specific links) then in a base class on your controller, set the Thread's locale. This example has . aspx pages not razor . cshtml.


2 Answers

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.

like image 120
Haacked Avatar answered Oct 07 '22 12:10

Haacked


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.

like image 22
user10479 Avatar answered Oct 07 '22 11:10

user10479