1:打开VS,新建ASP.NET MVC4项目

2:创建一个放本地化资源的文件夹并命名为"Language",右键选择添加新项,选择资源文件并命名为"Com"

把访问修饰符从internal修改为public, 要不其他类库访问不了。

3.同样道理 ,在"Language"目录中添加特定语言的资源文件。如Com.zh-CN.resx,Com.en-US.resx等。

4.新建IHttpModule接口实现类,以便访问Web时能根据语言动态加载资源文件。

namespace MvcApplication1.Language
{
    public class CultureAwareHttpModule : IHttpModule
    {
        #region Field

        private CultureInfo _currentCulture;

        private CultureInfo _currentUiCulture;

        #endregion

        #region IHttpModule

        public void Init(HttpApplication context)
        {
            context.BeginRequest += SetCurrentCulture;
            context.EndRequest += RecoverCulture;
        }

        public void Dispose()
        {

        }

        #endregion

        #region Method

        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))
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo(culture.ToString());
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture.ToString());
            }
        }

        private void RecoverCulture(Object sender, EventArgs args)
        {
            Thread.CurrentThread.CurrentCulture = _currentCulture;
            Thread.CurrentThread.CurrentUICulture = _currentUiCulture;
        }

        #endregion
    }
}

5.注册HttpModule

namespace MvcApplication1.Infrastructure
{
    public class PreApplicationStartCode
    {
        private static bool _hasLoaded;

        public static void PreStart()
        {
            if (!_hasLoaded)
            {
                _hasLoaded = true;
//注意这里的动态注册,此静态方法在Microsoft.Web.Infrastructure.DynamicModuleHelper DynamicModuleUtility.RegisterModule(typeof(CultureAwareHttpModule)); } } } }
[assembly: PreApplicationStartMethod(typeof(PreApplicationStartCode), "PreStart")]

6.创建页面

@using MvcApplication1.Language;

@{
    ViewBag.Title = "Index";
}

<h2>@Com.Create</h2>

<h2>@Com.Edit</h2>

7.定义路由

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

            routes.MapRoute(
               "Globalization",
               "{lang}/{controller}/{action}/{id}",
               new { lang = "zh",controller = "Home", action = "Index", id = UrlParameter.Optional },
               new { lang = "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$" }
           );

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

8.效果演示

 

 

 

 

 

posted on 2016-05-05 19:56  JustYong  阅读(911)  评论(0编辑  收藏  举报