代码改变世界

ASP.NET MVC中多语言的解决方案

2012-05-08 18:37  音乐让我说  阅读(593)  评论(2编辑  收藏  举报

转载自:在ASP.NET MVC中通过URL路由实现对多语言的支持

直接贴代码了!

    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("culture", 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;
        }

 

    <!-- 配置 HttpModule -->
    <httpModules>
      <add name="CultureAwareHttpModule" type="MvcApp.CultureAwareHttpModule, MvcApp"/>
    </httpModules>

 

配置路由

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{culture}/{controller}/{action}/{id}",
                defaults: new { culture = "en", controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

        }

 

新建资源文件 Resources.resx、Resources.zh.resx,并且把下面的属性改为 public

 

资源文件后台代码

        /// <summary>
        ///   Looks up a localized string similar to Cancel.
        /// </summary>
        public static string Cancel {
            get {
                return ResourceManager.GetString("Cancel", resourceCulture);
            }
        }
        
        /// <summary>
        ///   Looks up a localized string similar to Login.
        /// </summary>
        public static string Login {
            get {
                return ResourceManager.GetString("Login", resourceCulture);
            }
        }
        
        /// <summary>
        ///   Looks up a localized string similar to Passord.
        /// </summary>
        public static string Password {
            get {
                return ResourceManager.GetString("Password", resourceCulture);
            }
        }
        
        /// <summary>
        ///   Looks up a localized string similar to User Name.
        /// </summary>
        public static string UserName {
            get {
                return ResourceManager.GetString("UserName", resourceCulture);
            }
        }

新建 loginInfo 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using MvcApp.Properties;

namespace MvcApp.Models
{
    public class LoginInfo
    {
        [Display(Name ="UserName", ResourceType = typeof(Resources))]
        public string UserName { get; set; }

        [Display(Name="Password", ResourceType = typeof(Resources))]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
}

 

HomeController 中

using MvcApp.Models;

namespace MvcApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new LoginInfo());
        }
    }
}

 

View 中

@{
    ViewBag.Title = "主页";
}
@model MvcApp.Models.LoginInfo
@using (Html.BeginForm())
{ 
    @Html.EditorForModel()
    <input type="submit" value="@MvcApp.Properties.Resources.Login" />
    <input type="button" value="@MvcApp.Properties.Resources.Cancel" />
}

 

 

Demo 下载:https://files.cnblogs.com/Music/Asp_Net_Mvc_Mutil_language.rar

谢谢浏览!