Original link: https://www.cnblogs.com/ymnets/p/3584112.html
Our systems sometimes be extended to other countries or regions, need more locales, Microsoft provides a number of solutions, we are using the original js controlled, now it is not necessary.
As long as we create a simple resource file, you can easily switch languages by routing settings MVC.
This section benefit: Asp.net MVC3 Advanced Programming on page 121. We can Baidu own book, this should be the first of the Chinese version of the tutorial MVC3.0
Now from the project to start it (this section is also suitable for other MVC program), create a new project to put the language resource file.
First, the new App.Lang, while new BaseRes.resx and BaseRes.en.resx or other languages
Namely, Chinese, English. And reference the System.Web library i
Second, the communication process, the configuration App.Admin web.config, let the class take effect
Core files App.Admin folders and files inherit add CultureAwareHttpModule IHttpModule
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Routing; namespace App.Admin { public class CultureAwareHttpModule : IHttpModule { private CultureInfo currentCulture; private CultureInfo currentUICulture; public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += SetCurrentCulture; context.EndRequest += RecoverCulture; } private void SetCurrentCulture(object sender, EventArgs args) { currentCulture = Thread.CurrentThread.CurrentCulture; currentUICulture = Thread.CurrentThread.CurrentUICulture; HttpContextBase contextWrapper = new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper); if (routeData == null) { return; } object culture; if (routeData.Values.TryGetValue("lang", out culture)) { try { Thread.CurrentThread.CurrentCulture = new CultureInfo(culture.ToString()); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture.ToString()); } catch { } } } private void RecoverCulture(object sender, EventArgs args) { Thread.CurrentThread.CurrentCulture = currentCulture; Thread.CurrentThread.CurrentUICulture = currentUICulture; } } }
Here it must be declared: below 2 Paragraphs support MVC3, the second paragraph of support MVC4
----------------------- MVC3.0
<system.web>
<httpModules>
<add name="CultureAwareHttpModule" type=" App.Admin.CultureAwareHttpModule,App.Admin"/>
</httpModules>
</system.web>
----------------------- MVC4.0 (VS2012 version of the configuration of the following, MVC4 version of VS2010 configuration with MVC3.0)
<system.webServer>
<modules>
<add name="CultureAwareHttpModule" type="App.Admin.CultureAwareHttpModule,App.Admin"/>
</modules>
</system.webServer>
System.web node in the red section, type contains the namespace
Third, the registered route
Open RouteConfig.cs, registered as
static void the RegisterRoutes public (the RouteCollection routes) { routes.IgnoreRoute ( "Resource} {.axd / pathInfo {*}"); routes.MapRoute ( "Globalization", // Route Name "{lang} / {controller} / {action } / {id} ", // parameters with the URL new {lang =" ZH ", Controller =" Home ", Action =" Index ", ID = UrlParameter.Optional}, // default value of the parameter new {lang = "? ^ [a-zA- Z] {2} (- [a-zA-Z] {2}) $"} // parameter constraint ); routes.MapRoute ( "the Default", // route name "{controller } / {action} / {id } ",URL // with parameters new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Default parameters ); }
There has to perform routing we know everything. Finally, we can see that the visit would be like this
http://localhost:1201/(http://localhost:1201/zh),http://localhost:1201/等
Fourth, the project will be localized references App.Lang
Back Resx file, open access modifier Resx code set to public, and add the following property can be seen that a key value
Here we are with the index view SysSample example, the following code modifications back to the index
Introduced first @using App.Lang; then modify the following code
<div class="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>
BaseRes.Query which is the international property
Preview the example (please note my URL address changes)
Now you can localize your project. The last statement, of course, if you want to get the selected language is what you must refer to the page
CultureInfo info = Thread.CurrentThread.CurrentCulture;
Zh en or you can get to select the URL by info.Name
Example:
src='/@info.Name/SysSample/Create'
result
src='/en/SysSample/Create'