
此方式可以通过更改进行更改进程语言设定,支持从系统获取默认的区域设定,支持自定义,自定义的方式可以为cookie,可为资料库获取,session等方式。
具体怎么设定就看个人需要了。
第一步:
添加资源文件,添加的时候要将资源文件类型设定为public
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQ0AAAAzCAIAAAACd8o2AAAEO0lEQVR4nO2cT2rbQBSHc5bsBQYvfICAIS3tbhAYsozJwmQ9CON7DGN8jZHdbbftbnSOzg26GGn+Sn6y3cZO8vv4LaRYtlQyn997UuhdUUwQBDmeu6tfAYLcfuAJgtCBJ2Pz8DBHXK7+63jjDHpyf38/n3+5+vXdTh4e5tPpDJlOZ/DEB54kgSfwpCfwJAk8gSc9gSdJ4Ak86ckRT5jQigc/4XW0W0yKYsKVFmxSMKlNhpas97281qI875/BRC1YeEky2v0XOdeTp23TbBf5zzd7c1hfe8XDk5E5s560GgzsFsWkKEqhTe5PUZRCeU+4qnm7XalYpu4DKxHIExwfJnxvzYtSBHb6a2BSm58/2pd+/zL5NR9L7MnTtvGnaLZP53uy2DWm94DbDTzxGdF3JSs7Wt8DC3oSe1IpVRW8VkJqU3NXW+Ii46uQ244dsMXEVTm7m6lbKWMvKd8YlcwTt7g3e2P263M9eYeBJz6DnthWSsdrPTvMesKEjh0qI094bawn3L3LLn//gVzYgyultbY/Z1KF7ZnbtRuueWNSB58T9IpeDyb0+E5v2JPZYtsMlxR48hFyXj3JasKgJ+HYYDso995SKK19PXH1wXZN7XgjeCmUZLxWouSxddGcU1Qq2k10KoUOG7xum0lt38Wkptow2pPFrml2i/QAu7HZd1fdVR7nSSiMP+xoLwdP3tyT5+fnx8evF3mSVoyRnlRCSBXXk74veH+u9tNaAZI5xyje2wda90KLwnbrhNZr0BM3YAx6YowzYbFrUj3CjSP921tnuXwRAcvly6f25Nu3770vjPUk+tr2M/cITyZ+PuHt5N02Zjpcu7QnXBmTV4P4wgY8Sc51kifBGO+EGawnfv2t91aGzJP1wew3V9cjzGr1aiVZrV4/ez0ZemGsJ+HMHSzNUfOJ96T9BCa0XfFMSJ6dq98TJrWq3Eta9wv8HzzJpo7Ik83e9HrytG3ejSdWlUQSeHKiJ7xWPL6vFU3kp9zviupJ0n215zKBDEpIFc8nvnAxqa0GJ80nI0J74t2YTdcHY4K+ywmwPphRfddmi/nklnL28xNjb1VF48T5nnSvMqmDISc6V+GPUdlNqvQ5Y3ZYd7/LPVppy52/OXbBHO+ycN3Yfhf1XdtDV1PDkT2b4xe7Jj3sFgNPfOjnJ8GN166/8m70e2IfgPhF7+6Vdct39Ld7ktAT1+zFTznzkf2S5yefOvDE5wP+fReT2vgnleqi5/GfOvDE5wN6clngCTzpCTxJAk/gSU/gSRJ4Ak96Ak+SwBN4gtC5+n/dcFO5+q/jjQNPEIQOPEEQOvAEQejc/QEAUMATAGjgCQA08AQAGngCAA08AYAGngBAA08AoIEnANDAEwBo4AkANPAEABp4AgANPAGABp4AQANPAKCBJwDQwBMAaOAJADTwBAAaeAIADTwBgAaeAEDzFzuwiqxgOey5AAAAAElFTkSuQmCC" alt="" />
第二步:
新定义类:
namespace System.Web.Mvc
{
public static class LangHelper
{
//界面普通文字的多语言
public static string GetLangbyKey(this HtmlHelper htmlhelper, string key)
{
string lang = "zh-CN"; Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture; Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "zh-CN") ? typeof(WebApplication4.Resources.zh_CN) : typeof(WebApplication4.Resources.zh_TW); PropertyInfo p = resourceType.GetProperty(key);
if (p != null)
return p.GetValue(null, null).ToString();
else
return "undefined";
} //js定义多语言弹出框
public static string LangOutJsVar(this HtmlHelper htmlhelper, string key)
{
string lang = "zh-CN"; Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture; Type resourceType = (Thread.CurrentThread.CurrentUICulture.Name == "zh-CN") ? typeof(WebApplication4.Resources.zh_CN) : typeof(WebApplication4.Resources.zh_TW);
PropertyInfo p = resourceType.GetProperty(key);
if (p != null)
return string.Format("var {0} = '{1}'", key, p.GetValue(null, null).ToString());
else
return string.Format("var {0} = '{1}'", key, "undefined");
}
}
}
页面使用方法
<h2>@Html.GetLangbyKey("name")</h2>