原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)

我们的系统有时要扩展到其他国家,或者地区,需要更多的语言环境,微软提供了一些解决方案,原始我们是用js来控制的,现在不需要了。

我们只要创建简单的资源文件,通过MVC的路由设置就可以轻松的进行语言中的切换。

本节受益于:Asp.net MVC3 高级编程第121页。大家可以自行百度这本书,这应该是国内第一本中文版的MVC3.0教程

现在从项目入手吧(本节也适合其他MVC程序),新建一个语言项目来放资源文件。

一、新建App.Lang,同时新建BaseRes.resx和BaseRes.en.resx或者其他国语言

分别是中文,英文。并引用System.Web类库i

二、处理通讯,配置App.Admin web.config,让这个类生效

在App.Admin中的Core文件夹添加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;
}
}
}

CultureAwareHttpModule

这里必须做个声明:下面2段第一段支持MVC3,第二段支持MVC4

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

<system.web>

<httpModules>
<add name="CultureAwareHttpModule" type=" App.Admin.CultureAwareHttpModule,App.Admin"/>
</httpModules>
</system.web>

-----------------------MVC4.0 (VS2012 版本配置以下, VS2010的MVC4版本配置同MVC3.0)

<system.webServer>
<modules>
<add name="CultureAwareHttpModule" type="App.Admin.CultureAwareHttpModule,App.Admin"/>
</modules>

</system.webServer>

红色部分在system.web节点内,type包含的是命名空间

三、注册路由

打开RouteConfig.cs,注册为

   public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
"Globalization", // 路由名称
"{lang}/{controller}/{action}/{id}", // 带有参数的 URL
new { lang = "zh", controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
new { lang = "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$" } //参数约束
); routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
); }

路由执行有先后大家都懂的。可以看出最后我们的访问会是这样的

http://localhost:1201/(http://localhost:1201/zh),http://localhost:1201/等

四、将要本地化的项目引用App.Lang

回到Resx文件,打开Resx设置代码为的访问修饰符为public,并添加如下属性,可以看出是键值对应

这里我们以SysSample的index视图为例,回到index上修改如下代码

先引入@using App.Lang;然后修改以下代码

<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就是国际化属性了

预览一下例子(请注意我的URL地址变化)

现在你可以本地化您的项目了。最后一个声明,如果你要获取当然选中的是什么语言你必须在页面引用

CultureInfo info = Thread.CurrentThread.CurrentCulture;

通过info.Name可以获取到URL上选择的zh或en

例:

src='/@info.Name/SysSample/Create'

结果

src='/en/SysSample/Create'

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)的更多相关文章

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

  2. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充 系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支 系列目录 步骤设置完毕之后,就要设置好流转了,比如财务申请大于50000元( ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-【过滤器+Cache】

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-[过滤器+Cache] 系列目录 上次的探讨没有任何结果,我浏览了大量的文章 ...

  10. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...

随机推荐

  1. javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等

    使用device.js检测设备并实现不同设备展示不同网页 html代码: <!doctype html> <html> <head> <meta charse ...

  2. 解决SDK Manager无法更新问题

    因为google被封了,导致Android SDK Manager无法更新,解决方案如下: 1.选择tools->options,跳出Settings页面 2.设置HTTP Proxy代理,设置 ...

  3. 初级SQL开发汇总指南

    汇总部分内容来自网络(作者  :zhtbs),比较基础的东西,能够了解比较基础的一些东西. Select语句概要 数据库中数据的提取(查询)使用select 语法,主要有以下几点作用 l  提取的数据 ...

  4. Web Adaptor重装配置时 提示已经配置成功的问题

    环境 ArcGIS 10.1/10.2/10.3 Windwos 8.1 Tomcat 7.0.5 问题描述 较早之前在本机上安装配置过一个10.2.1版本的ArcGIS产品,包括桌面.Server和 ...

  5. BZOJ 3676 回文串

    Description 考虑一个只包含小写拉丁字母的字符串\(s\).我们定义\(s\)的一个子串\(t\)的"出现值"为\(t\)在\(s\)中的出现次数乘以\(t\)的长度.请 ...

  6. xcode 环境,多工程联编设置【转】

    http://blog.csdn.net/vienna_zj/article/details/8467522 一.xcode4中的环境变量 $(BUILT_PRODUCTS_DIR) build成功后 ...

  7. NWERC 2012 Problem E Edge Case

    比赛的时候刷了一点小聪明,发现这个数列是卢卡斯数,一个递推关系像斐波拉契数列的数列: 我不知道怎么证明,如果哪天无意中会证了再加上: 这题唯一的难点就是大数运算: 直接用JAVA 代码: import ...

  8. GConf 错误:联系配置服务器失败;某些可能原因是需要为 ORBit 启用 TCP/IP 联网

    http://blog.csdn.net/heirenheiren/article/details/8107294 http://blog.sina.com.cn/s/blog_8097f8aa010 ...

  9. 【poj1087/uva753】A Plug for UNIX(最大流)

    A Plug for UNIX   Description You are in charge of setting up the press room for the inaugural meeti ...

  10. MySQL FEDERATED引擎使用示例, 类似Oracle DBLINK

    原文地址:http://it.dataguru.cn/article-3352-1.html 摘要: 本地MySQL数据库要访问远程MySQL数据库的表中的数据, 必须通过FEDERATED存储引擎来 ...