zoukankan      html  css  js  c++  java
  • ASP.NET MVC 多语言解决方案

    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.效果演示

  • 相关阅读:
    MYSQL长连接的一个小问题
    wpf关于PasswordBox的绑定Password的问题(MVVM框架)
    关于C# DataGridView 全选与取消的小问题
    谈谈在京东手机APP的产品推荐
    赵海平给中国开发者的建议
    python_闭包_匿名函数
    mysql远程连接10038错误
    计算机系统 -- 计算机
    Java——类和对象
    ORACLE数据库定时任务—DBMS_JOB
  • 原文地址:https://www.cnblogs.com/JustYong/p/5463167.html
Copyright © 2011-2022 走看看