Build a backend management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (30)-Localization (multi-language)

Source: Internet
Author: User

Original: Building a backend management system for ASP. Mvc4+ef5+easyui+unity2.x Injection (30)-Localization (multi-language)

Our system is sometimes extended to other countries, or regions, need more language environment, Microsoft provides some solutions, the original we are using JS to control, now do not need.

As long as we create a simple resource file, we can easily switch the language by using MVC's routing settings.

This section benefits from: ASP. MVC3 Advanced Programming Page 121th. Everyone can own Baidu this book, this should be the first Chinese version of the MVC3.0 tutorial

Now start with the project (this section is also suitable for other MVC programs) and create a new language project to put the resource files.

First, new App.lang, while new Baseres.resx and BaseRes.en.resx or other national languages

They are in Chinese and English respectively. and referencing the System.Web class library I

Second, the processing of communications, configuration App.admin Web. config, let this class take effect

Add cultureawarehttpmodule files to the core folder in App.admin and inherit IHttpModule

usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingsystem.web;usingSystem.Web.Routing;namespaceapp.admin{ Public classCultureawarehttpmodule:ihttpmodule {PrivateCultureInfo currentCulture; PrivateCultureInfo CurrentUICulture;  Public voidDispose () {} Public voidInit (HttpApplication context) {context. BeginRequest+=setcurrentculture; Context. EndRequest+=recoverculture; }        Private voidSetcurrentculture (Objectsender, EventArgs args) {CurrentCulture=Thread.CurrentThread.CurrentCulture; CurrentUICulture=Thread.CurrentThread.CurrentUICulture; HttpContextBase Contextwrapper=NewHttpcontextwrapper (httpcontext.current); Routedata Routedata=RouteTable.Routes.GetRouteData (Contextwrapper); if(Routedata = =NULL)            {                return; }            Objectculture; if(RouteData.Values.TryGetValue ("Lang", outculture)) {                Try{Thread.CurrentThread.CurrentCulture=NewCultureInfo (culture.                    ToString ()); Thread.CurrentThread.CurrentUICulture=NewCultureInfo (culture.                ToString ()); }                Catch                { }            }        }        Private voidRecoverculture (Objectsender, EventArgs args) {Thread.CurrentThread.CurrentCulture=currentCulture; Thread.CurrentThread.CurrentUICulture=CurrentUICulture; }    }}
Cultureawarehttpmodule

A statement must be made here: the first paragraph of the following 2 paragraphs supports MVC3 and the second paragraph supports MVC4

-----------------------MVC3.0

<system.web>


<add name= "Cultureawarehttpmodule" type= "App.admin.cultureawarehttpmodule,app.admin"/>

</system.web>

-----------------------MVC4.0 (VS2012 version configured below, VS2010 MVC4 version configured with MVC3.0)

<system.webServer>
<modules>
<add name= "Cultureawarehttpmodule" type= "App.admin.cultureawarehttpmodule,app.admin"/>
</modules>

</system.webServer>

The red part is within the system.web node, and the type contains the namespace

Third, registered routing

Open RouteConfig.cs, register as

    Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute ("Globalization",//Route name                "{Lang}/{controller}/{action}/{id}",//URL with Parameters                New{lang ="ZH", CONTROLLER ="Home", action ="Index", id = urlparameter.optional},//parameter Default value                New{lang ="^[a-za-z]{2} (-[a-za-z]{2})? $"}//parameter Constraints            ); Routes. MapRoute ("Default",//Route name                "{Controller}/{action}/{id}",//URL with Parameters                New{controller ="Home", action ="Index", id = urlparameter.optional}//parameter Default value            ); }

Routing execution has been understood by everyone. We can see that at the end of our visit,

HTTP://LOCALHOST:1201/(Http://localhost:1201/zh), http://localhost:1201/, etc.

Iv. projects that will be localized reference App.lang

Go back to the ResX file, open the ResX settings code for the access modifier to public, and add the following properties to see that the key value corresponds

Here we take the Syssample index view as an example, back to index to modify the following code

First introduce @using App.lang; then modify the following code

<divclass="Mvctool"> <input id="Txtquery"Type="text" class="SearchText"/>@Html. Toolbutton ("Btnquery","Icon-search", Baseres.query, Perm,"Query",true) @Html. Toolbutton ("btncreate","Icon-add", Baseres.create, Perm,"Create",true) @Html. Toolbutton ("Btnedit","Icon-edit", Baseres.edit, Perm,"Edit",true) @Html. Toolbutton ("btndetails","icon-details", Baseres.details, Perm,"Details",true) @Html. Toolbutton ("Btndelete","Icon-remove", Baseres.delete, Perm,"Delete",true) @Html. Toolbutton ("Btnexport","Icon-export", Baseres.export, Perm,"Export",true)</div>

The baseres.query is the internationalization attribute.

Preview The example (please note my URL address changes)

Now you can localize your project. The last statement, if you want to get the selected language of course you have to reference in the page

CultureInfo info = Thread.CurrentThread.CurrentCulture;

Via info. Name can be obtained to the URL of the selected en or en

Cases:

src='/@info. Name/syssample/create'

Results

src='/en/syssample/create'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.