ASP.Net MVC多語言

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   java   

.NET MVC 多語言網站

通過瀏覽器語言喜好設定改變MVC的語言,通過瀏覽器語言選項,修改指令碼語言。

一、添加資源檔

1、添加App_GlobalResources檔案夾。

 

 

2、添加預設的資源檔和對應的語言碼資源檔。如zh-cn代表中國大陸,en-us代表美製英語。詳情:http://www.lingoes.cn/zh/translator/langcode.htm

 

 

3、將資源檔設定成public。建立時預設是internal,這樣不能被訪問。

右鍵點擊資源檔,在其的屬性中將自訂工具(Custom Tool) 從資源代理產生器(GlobalResourceProxyGenerator) 改為 公用檔案(PublicResXFileCodeGenerator),然後將產生操作從內容改為內嵌資源。

 

4、在web.config中添加語言選項。這樣就會根據瀏覽器語言喜好設定,判斷要使用的資源。

    <!--根據瀏覽器的語言喜好設定,決定語言項--><globalization uiCulture="auto" culture="auto" enableClientBasedCulture="true" />

 

5、在對應的檔案中添加索引值對。

 

 

二、View+Control+Model的普通文字多語言

1、view

 

2、control

 

3、model

//[Required(ErrorMessage = "密碼不可為空")] [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "PasswordNotNull")]

 

三、DisplayName的多語言化。重寫DisplayName方法

 

  public class LocalDisplayName : DisplayNameAttribute    {        private string _defaultName = "";        public Type ResourceType { get; set; }
public string ResourceName { get; set; } public LocalDisplayName(string defaultName) { _defaultName = defaultName; } public override string DisplayName { get { PropertyInfo p = ResourceType.GetProperty(ResourceName); if (p != null) { return p.GetValue(null, null).ToString(); } else { return _defaultName; } } } }

 

model使用:

 [LocalDisplayName("發布時間", ResourceName = "PublishDateTime", ResourceType = typeof(Resource))]

 

 

四、指令碼多語言

我的方法是在引用一個公用的指令檔,由該檔案通過當前語言引入對應的語言資源指令碼。如果當前瀏覽區語言沒有沒有對應的資源指令碼,就引入預設的資源指令碼。

 

1、通過瀏覽器語言引入對應的語言資源指令碼。

//將所有的語言資源指令檔名羅列再次,用&分割var languageresources = "en-us&zh-cn" //根據當前瀏覽器語言驗證語言指令碼是否存在,如果在則載入,則載入預設的zh-cn.jsvar thislanguagere = navigator.language.toLowerCase(); var script = document.getElementsByTagName(‘HEAD‘).item(0);script = document.createElement("script");script.type = "text/javascript"; var language = languageresources.split("&")var flag = false;for (var item in language) {    if (thislanguagere == language[item]) {        var flag = true;        script.src = "/Scripts/controls/" + thislanguagere + ".js";        document.body.appendChild(script);    }}if (flag == false) {    script.src = "/Scripts/controls/zh-cn.js";    document.body.appendChild(script);}

 

 

2、對應資源指令碼寫法。設定索引值對

 

 

 

3、具體頁面指令碼使用,引用。需要先引用判斷語言指令碼。

 

 

 

另附:可修改的多語言,即通過語言超連結修改當前語言項。

1、在Global.asax檔案中:

    

   protected void Application_AcquireRequestState(Object sender, EventArgs e)        {            Authentication.AttachAuthTicketByUrl<WebAuthentication>(Context);            //多語言 可切換            if (HttpContext.Current.Session != null)            {                System.Globalization.CultureInfo ci = (System.Globalization.CultureInfo)this.Session["CurrentLanguage"];                if (ci == null)                {                    ci = new System.Globalization.CultureInfo("zh-cn");                    this.Session["CurrentLanguage"] = ci;                }                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;                System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(ci.Name);            }        }

 

2、在control中:

        public ActionResult ChangeLanguage(string language)        {            Session["CurrentLanguage"] =new System.Globalization.CultureInfo(language);            return Redirect("Login");        }

 

3、在頁面中:

@Html.ActionLink("English", "ChangeLanguage", "Account", new { language = "en-us" }, new { @class = "hex-a" })@Html.ActionLink("Chinese", "ChangeLanguage", "Account", new { language = "zh-cn" }, new { @class = "hex-a" })

 

 

參考資料:

.NET MVC 2 多語言網站的實現: 

 http://blog.163.com/xu_shuhao/blog/static/52577487201092402610920/

 

關於在mvc4中多語言建站的執行個體:

http://www.cnblogs.com/Joans/archive/2012/08/16/2640473.html

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.