来源: 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<DictionaryEntry>().FirstOrDefault<DictionaryEntry>(x => 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">