代码:using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Threading;using System.Web;using System.Web.Mvc;namespace MvcResourceLang.Models {public static class LangHelper {//界面普通文字的多语言public static string GetLangbyKey(this HtmlHelper htmlhelper, string key) {Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "en-US") ? typeof(Resources.en_US) : typeof(Resources.zh_CN);PropertyInfo p = resourceType.GetProperty(key);if (p != null)return p.GetValue(null, null).ToString();elsereturn "undefined";}//js定义多语言弹出框public static string LangOutJsVar(this HtmlHelper htmlhelper, string key) {Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "en-US") ? typeof(Resources.en_US) : typeof(Resources.zh_CN);PropertyInfo p = resourceType.GetProperty(key);if (p != null)return string.Format("var {0} = '{1}'", key, p.GetValue(null, null).ToString());elsereturn string.Format("var {0} = '{1}'", key, "undefined");}}}
前台:<body> <div> @{ string controller = ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString(); string action = ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString(); //string lang = ViewContext.Controller.ValueProvider.GetValue("lang").RawValue.ToString(); } @Html.ActionLink("中文", action, new { Controller = controller, lang = "zh-CN" }, new { @class = "btn-a" }) @Html.ActionLink("English", action, new { Controller = controller, lang = "en-US" }, new { @class = "btn-a" }) </div> <h2>@Html.GetLangbyKey("LogonDisplay")</h2> <div class="logon-div"> @using (Html.BeginForm()) { <table> <tr> <td>@Html.LabelFor(m => m.UserName)</td> <td>@Html.TextBoxFor(m => m.UserName, new { style = "width:100%" })</td> </tr> <tr> <td>@Html.LabelFor(m => m.Pwd)</td> <td>@Html.PasswordFor(m => m.Pwd, new { style = "width:100%" })</td> </tr> <tr> <td colspan="2">@Html.CheckBoxFor(m => m.Rememberme) @Html.LabelFor(m => m.Rememberme) <input type="submit" value="@Html.GetLangbyKey("LogonDisplay")" style="float:right;"/> </tr> <tr style="font-size: 12px;"> <td colspan="2"> @Html.GetLangbyKey("NoteDisplay") @Html.ActionLink(Html.GetLangbyKey("RegisterDisplay"), "Register", null, null) . </tr> </table> } </div></body>
评论