Asp.net MVC 多语言应用 - finly - 博客园

来源: Asp.net MVC 多语言应用 – finly – 博客园

这里以Mvc3模版项目的登录页为例,简单说一下过程:

首先准备资源文件,即语言包。为web site项目添加Resource文件夹,然后在Resource文件夹下添加两个resx文件

命令行工具ResGen.exe将这两个resx文件生成同名的resources文件,如zh-CN.resources、en-US.resources,生成后将这两个resources文件放到Resource目录下

写一个静态getLang

<div class="container">
<div class="line number1 index0 alt2"><code class="csharp keyword">namespace</code> <code class="csharp plain">System.Web.Mvc</code></div>
<div class="line number2 index1 alt1"><code class="csharp plain">{</code></div>
<div class="line number3 index2 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">using</code> <code class="csharp plain">System.Collections;</code></div>
<div class="line number4 index3 alt1"><code class="csharp spaces">    </code><code class="csharp keyword">using</code> <code class="csharp plain">System.Resources;</code></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">using</code> <code class="csharp plain">System.Linq;</code></div>
<div class="line number6 index5 alt1"></div>
<div class="line number7 index6 alt2"><code class="csharp spaces">    </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">class</code> <code class="csharp plain">LocalizationHelper</code></div>
<div class="line number8 index7 alt1"><code class="csharp spaces">    </code><code class="csharp plain">{</code></div>
<div class="line number9 index8 alt2"><code class="csharp spaces">        </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">string</code> <code class="csharp plain">Lang(</code><code class="csharp keyword">this</code> <code class="csharp plain">HtmlHelper html, </code><code class="csharp keyword">string</code> <code class="csharp plain">key)</code></div>
<div class="line number10 index9 alt1"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number11 index10 alt2"><code class="csharp spaces">            </code><code class="csharp keyword">return</code> <code class="csharp plain">GetLang(key);</code></div>
<div class="line number12 index11 alt1"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number13 index12 alt2"></div>
<div class="line number14 index13 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">string</code> <code class="csharp plain">GetLang(</code><code class="csharp keyword">string</code> <code class="csharp plain">key)</code></div>
<div class="line number15 index14 alt2"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number16 index15 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">var</code> <code class="csharp plain">filePath = HttpContext.Current.Server.MapPath(</code><code class="csharp string">"~/Resource"</code><code class="csharp plain">);</code></div>
<div class="line number17 index16 alt2"><code class="csharp spaces">            </code><code class="csharp keyword">string</code> <code class="csharp plain">language = HttpContext.Current.Session[</code><code class="csharp string">"CurrentLanguage"</code><code class="csharp plain">] == </code><code class="csharp keyword">null</code> <code class="csharp plain">?</code></div>
<div class="line number18 index17 alt1"><code class="csharp spaces">                </code><code class="csharp string">"zh-CN"</code> <code class="csharp plain">: HttpContext.Current.Session[</code><code class="csharp string">"CurrentLanguage"</code><code class="csharp plain">].ToString();</code></div>
<div class="line number19 index18 alt2"><code class="csharp spaces">            </code><code class="csharp keyword">string</code> <code class="csharp plain">resxPath = </code><code class="csharp keyword">string</code><code class="csharp plain">.Format(</code><code class="csharp string">@"{0}\{1}.resources"</code><code class="csharp plain">, filePath, language);</code></div>
<div class="line number20 index19 alt1"><code class="csharp spaces">            </code><code class="csharp plain">ResourceReader reader = </code><code class="csharp keyword">new</code> <code class="csharp plain">ResourceReader(resxPath);</code></div>
<div class="line number21 index20 alt2"><code class="csharp spaces">            </code><code class="csharp keyword">var</code> <code class="csharp plain">entry = reader.Cast&lt;DictionaryEntry&gt;().FirstOrDefault&lt;DictionaryEntry&gt;(x =&gt; x.Key.ToString() == key);</code></div>
<div class="line number22 index21 alt1"><code class="csharp spaces">            </code><code class="csharp plain">reader.Close();</code></div>
<div class="line number23 index22 alt2"><code class="csharp spaces">            </code><code class="csharp keyword">return</code> <code class="csharp plain">entry.Value == </code><code class="csharp keyword">null</code> <code class="csharp plain">? </code><code class="csharp string">""</code> <code class="csharp plain">: (</code><code class="csharp keyword">string</code><code class="csharp plain">)entry.Value;</code></div>
<div class="line number24 index23 alt1"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number25 index24 alt2"></div>
<div class="line number26 index25 alt1"><code class="csharp spaces">        </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp keyword">string</code> <code class="csharp plain">GetLanguage(</code><code class="csharp keyword">this</code> <code class="csharp plain">HtmlHelper html)</code></div>
<div class="line number27 index26 alt2"><code class="csharp spaces">        </code><code class="csharp plain">{</code></div>
<div class="line number28 index27 alt1"><code class="csharp spaces">            </code><code class="csharp keyword">return</code> <code class="csharp plain">HttpContext.Current.Session[</code><code class="csharp string">"CurrentLanguage"</code><code class="csharp plain">].ToString();</code></div>
<div class="line number29 index28 alt2"><code class="csharp spaces">        </code><code class="csharp plain">}</code></div>
<div class="line number30 index29 alt1"><code class="csharp spaces">    </code><code class="csharp plain">}</code></div>
<div class="line number31 index30 alt2"><code class="csharp plain">}</code></div>
<div class="line number31 index30 alt2">

 第二步、为动态切换语言,要在Global.asax文件中添加Application_AcquireRequestState事件,如:

复制代码

</div>
<div class="cnblogs_code_toolbar">
<pre>protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session != null)
            {
                System.Globalization.CultureInfo ci =
                    (System.Globalization.CultureInfo)this.Session["CurrentLanguage"];
                if (ci == null)
                {
                    ci = new System.Globalization.CultureInfo(Request.UserLanguages[0].ToString());
                    this.Session["CurrentLanguage"] = ci;
                }
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
                System.Threading.Thread.CurrentThread.CurrentCulture =
                    System.Globalization.CultureInfo.CreateSpecificCulture(ci.Name);
            }
        }</pre>
<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码"><img src="http://www.mikel.cn/wp-content/uploads/2015/04/copycode29.gif" alt="复制代码" /></a></span></div>
<div class="cnblogs_code_toolbar">
第三步、在HomeController中添加ChangeLanguage方法,很简单、就一句代码,如:
</div>
<div class="cnblogs_code_toolbar">
<div class="container">
<div class="line number1 index0 alt2"><code class="csharp keyword">public</code> <code class="csharp plain">JsonResult ChangeLanguage()</code></div>
<div class="line number2 index1 alt1"><code class="csharp spaces">       </code><code class="csharp plain">{</code></div>
<div class="line number3 index2 alt2"><code class="csharp spaces">           </code><code class="csharp keyword">string</code> <code class="csharp plain">aa = Request[</code><code class="csharp string">"language"</code><code class="csharp plain">];</code></div>
<div class="line number4 index3 alt1"><code class="csharp spaces">           </code><code class="csharp plain">Session[</code><code class="csharp string">"CurrentLanguage"</code><code class="csharp plain">] = </code><code class="csharp keyword">new</code> <code class="csharp plain">System.Globalization.CultureInfo(Request[</code><code class="csharp string">"language"</code><code class="csharp plain">]);</code></div>
<div class="line number5 index4 alt2"><code class="csharp spaces">           </code><code class="csharp keyword">return</code> <code class="csharp plain">Json(</code><code class="csharp string">"操作成功!"</code><code class="csharp plain">, JsonRequestBehavior.AllowGet);</code></div>
<div class="line number6 index5 alt1"><code class="csharp spaces">       </code><code class="csharp plain">}</code></div>
<div class="line number6 index5 alt1">
赞(0) 打赏
分享到: 更多 (0)
标签:

相关推荐

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏