Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globalization in ASP.Net MVC 3

I am trying to achieve globalization/localization in my MVC 3 application. I don't want different Views for each language. Please suggest how I can proceed. Any supported links/URLs will be of great help.

like image 806
Vivek Avatar asked Apr 01 '11 06:04

Vivek


People also ask

What is globalization in MVC?

Globalization is the process of designing the application in such a way that it can be used by users from across the globe (multiple cultures). Localization, on the other hand, is the process of customization to make our application behave depending on the current culture and locale.

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.

Which choice best describes the difference between globalization and localization?

Globalization in a Nutshell. Localization is the adaptation of a resource or product to fit the demands of one specific culture or locale, while globalization is the adaption of a particular resource to fit the demands of multiple cultures and locales.

How will you set the culture and UI culture for ASP.NET web page globalization?

To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo.


1 Answers

You localize it in the same way as any other application like this:

  1. Create a folder, call it e.g. Resources
  2. Right click the folder and add class... choose resource file. Call it anything you like e.g. Strings.resx
  3. Under the properties of file, change Custom Tool to be PublicResXFileCodeGenerator
  4. Populate the Resource file with Translation key and value pairs (this will be the default translation)
  5. Create other resources with the name of the culture they're for in this format: {name}.de.resx e.g. Strings.de.resx
  6. (This is for Razor) crack open the web.config in the Views folder and add this to /configuration/system.web.webPages.razor/pages/namespaces: <add namespace="Resources" /> (assuming resources is the name of the folder you created the resources in and you haven't changed the default namespace on the resouce files themselves). This step means you don't have to fully qualify the resource classes in your views each time you want to reference a translation.
  7. Use the translations in place of text in your views like with the following code:

    @Strings.MyString 

Strings will be automatically translated in the view depending on CultureInfo.CurrentCulture but this is not set automatically for you

You will need to change the CurrentCulture (potentially in Application_BeginRequest). How you do this is up to you, it could be a route value which sets it or you can read the user's browser language

You can find a list of the user's prefered languages (in order) in HttpContext.Current.Request.UserLanguages.

like image 176
Martin Booth Avatar answered Nov 05 '22 19:11

Martin Booth