This article short address: http://zdd.me/aspnetmultilingual
Currently, most websites support multiple languages, making it easy for users in different languages to access the website. I used asp.net mvc 2 a few days ago to build a website that provides both Chinese and English support. Here I will share with you the multilingual Implementation Method of my website, I also hope that users with better implementation methods will be able to enlighten me.
Create an asp.net mvc 2 project. first look at the initial structure of the mvc 2 project:
- Views is used to present the user interface, usually aspx, ascx, or html files. This generally does not involve the logic processing of programs.
- Scripts is used to store js script files.
- Models contains the program's core data, which generally includes data entities, data verification, and database access.
- Controllers is a bridge between Models and Views, that is, collecting data from Views and handing it over to Models, and passing data from Models to Views.
- Content is generally used to store pictures and CSS files used in the program.
Add a Resources folder to the new project to store Resources files in multiple languages. To facilitate resource file management, add sub-directories such as Home, Account, Shared, and Models to the Resources folder to store the corresponding resource files. After the project is created, you can implement multi-language support.
This article mainly demonstrates how to implement multiple languages. Therefore, only the Site. Master page in the Shared view can be supported in multiple languages. Add a resource file in the Resources/Shared folder named Shared. resx, which stores the default language of the website (in simplified Chinese). Then, add a file named Shared. en-us.resx file, which enables the website to implement English support. The resource file is named in the format of <File Name>. <language name>. resx.
After adding the resource file, first change the Access Modifier of the resource file to Public. Then add the following string. Add the corresponding English in Shared. en-us.resx.
The default namespace of the resource file is the project name. Folder name. We will change its namespace to ViewResources.
Now we can use the content in the resource file on the page. Next we will change the character in Site. Master to the character in the resource file, as shown in:
Press Ctrl + F5 to run the program. You can see that the characters in Site. Master have been changed to Chinese. Switch between Chinese and English:
Add the following method to the Global. asax. cs file:
Protected void Application_AcquireRequestState (object sender, EventArgs e) {if (HttpContext. Current. Session! = Null) {CultureInfo cultureInfo = (CultureInfo) this. session ["Culture"]; // determines whether a value exists in the Session. if no value exists, set the default value if (cultureInfo = null) {string langName = "zh-cn "; if (HttpContext. current. request. userages! = Null & HttpContext. Current. Request. userages. Length! = 0) {langName = HttpContext. current. request. userages [0];} cultureInfo = new CultureInfo (langName); this. session ["Culture"] = cultureInfo;} System. threading. thread. currentThread. currentCulture = cultureInfo; System. threading. thread. currentThread. currentUICulture = cultureInfo; System. threading. thread. currentThread. currentCulture = CultureInfo. createSpecificCulture (cultureInfo. name); System. Threading. Thread. CurrentThread. CurrentUICulture = CultureInfo. CreateSpecificCulture (cultureInfo. Name) ;}} this method determines the language the user wants to use when accessing each page. We add the method to modify the language in HomeController:
/// <Summary> /// modify the language /// </summary> /// <param name = "lang"> the parameter lang is the language you want to use </param> /// <param name = "returnUrl"> parameter returnUrl: the page on which the user clicks to modify the language </param> /// <returns> </returns> public ActionResult ChangeCulture (string lang, string returnUrl) {Session ["Culture"] = new CultureInfo (lang); return Redirect (returnUrl );}
Then add the modified language connection to LogOnUserControl. ascx:
Now we have fully implemented the multi-language switch. Press Ctrl + F5 to run the program and you can switch between Chinese and English.
This example only enables switching between Chinese and English. To add more languages, you only need to add resource files.
The example program downloads mvcmultilingual.zip