Razor MVC Multi-language 多语言系统的实现的问题

来源:互联网 发布:安易数据恢复免费版 编辑:程序博客网 时间:2024/06/18 06:12

Razor MVC Multi-language 多语言系统的实现的问题

控件,自身支持,例如Telerik

根据控件对Resource文件的名称的要求,建立APP_GlobaResource目录下的文件

在web.config中修改UICulture等信息就完成了某个语言显示的设定

Globalization in MVC 3with Razor

 

主要

一、       处理Form Request,类似之前的ASP模式,通过Submit然后后台的ActionURL ASP页面接收参数,处理反馈。

基本要求:

1.前台有Form,有Submit,有提交的内容

@{Html.BeginForm("Index","Home");}<table cellpadding="0″cellspacing="0"style="border:0px none white; width:600px;"><tr><tdstyle="width:300px;"> <table cellspacing="0"cellpadding="0"style="border:0px none white; width:300px;"><tr><tdstyle="width:150px;text-align:left;">Language</td><tdstyle="width:150px;text-align:right;"><inputtype="submit"value="Update Language"/>

….

</table>@{Html.EndForm();}

 

2.后台控制器 Controller

一个Action,对应Form中的Html.BeginForm("Index","Home")

 public ActionResult Index(String language)        { …}
3.这个过程要理解刷新,或则page_load
         二、关于通过代码访问资源的方式:
使用接口(其中的Namespace就来自Customtoolnamespace)
<td style="width:300px;">@HWResources.ServerSide.HelloString</td>
定义方式:(设置很重要)

 

三、View Access of Local Resource using Html Extension Method

通过HTML help的扩展访问本地资源

Razor与传统的Web Form view引擎不一样,web form允许代码访问资源。这里讲如何通过HTML扩展来调用类似GetLocalResourceObject的方式:

 
主要是 page.ViewContext.HttpContext.GetLocalResourceObject
 
提一下使用HTMLHelper的步骤:
1. 创建一个来自html helper的扩展类
2. 注册到Razor viewEngine configuration entry,通过web.config,在
<System.Web.WebPages.Razor> 
记住这个是在view\web.config

 

这个方式总体来说的步骤:

1.  写Extension,第一个参数,以及确认使用LocaresourceObject,就是要来自APP_LocalResources目录

public static string LocalResources(this WebViewPage page, string key)        {            //如果需要图片资源,不过基本还是string为主            return page.ViewContext.HttpContext.GetLocalResourceObject(page.VirtualPath, key) as string;}

 

2.Razor view代码

<b>View Access of custom embedded resource using html extension</b></td><td style="width:300px;">@this.LocalResources("String1")</td>
 
LocalResources(this WebViewPage page=@this, string key= String1)

四、上述代码的简化版,但显然导致过多代码:

<b>View Access of Local Resource using Http Context GetLocalResourceObject ....</b></td><td style="width:300px;">@HttpContext.GetLocalResourceObject("~/Views/Home/Index.cshtml","String1")</td>

 

参数需要使用当前的View的页面。"~/Views/Home/Index.cshtml"

最后的效果: