ASP.NET MVC2 中使用多语言

25 篇文章 0 订阅
22 篇文章 0 订阅

1、创建一个 ASP.NET MVC 2空项目

然后添加Home控制器,HomeController类内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;

namespace MvcAJAX.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ChangeCulture(string lang, string returnUrl)
        {
            Session["Culture"] = new CultureInfo(lang);
            return Redirect(returnUrl);
        }

    }
}

 

2、为Home创建Index视图,内容如下:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
        <%=Html.ActionLink("English","ChangeCulture",new{lang="en-us",returnUrl=this.Request.RawUrl},null) %>
        <br />
        <%=Html.ActionLink("简体中文", "ChangeCulture", new { lang = "zh-cn", returnUrl = this.Request.RawUrl }, null)%>
        <br />
        [<%=ViewResources.Index.Name%>]
    </div>
</body>
</html>


 

3、添加资源文件

在根目录中添加Resources文件夹,在其中添加 Default 子文件夹,在Default中再添加Home子文件夹。

在Home中添加资源文件:Index.resx和Index.en-us.resx

把Index.resx命名空间定义为:ViewResources

修改两个文件的访问属性为:public

并为两个资源中都添加名称为Name的变量,分别为中文、英文字符串。

 

4、修改Global.asax文件内容,添加如下代码:


 

protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                CultureInfo cultureInfo = (CultureInfo)this.Session["Culture"];
                // 判断Session中是否有值,没有就设置默认值
                if (cultureInfo == null)
                {
                    string langName = "zh-cn";
                    if (HttpContext.Current.Request.UserLanguages != null && HttpContext.Current.Request.UserLanguages.Length != 0)
                    {
                        langName = HttpContext.Current.Request.UserLanguages[0];
                    }
                    cultureInfo = new CultureInfo(langName);
                    this.Session["Culture"] = cultureInfo;
                }

                Thread.CurrentThread.CurrentCulture = cultureInfo;
                Thread.CurrentThread.CurrentUICulture = cultureInfo;
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
            }
        }


       

 

5、访问 /Home/Index 测试效果

 

示例下载:http://download.csdn.net/detail/xz2001/4143788

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值