Asp.Net MVC 多語言(html+js共用一套資源檔)

來源:互聯網
上載者:User

標籤:window   lang   tla   Google   tar   ima   local   經驗   勿噴   

        此文主要是最近做多語言的工作的一個經驗分享。文中的內容為參照多位大神的方案後,自己揉捏出來的一個新的方案,對於html和javascript部分的多語言的切換,共用一套資源檔。代碼中主要是使用 IHttpModule + cookie 的方式實現。具體原理不多說(請問度娘或Google),旨在共用代碼。 如果有不喜歡的也請繞行,勿噴!

        此方案比較簡單易懂,也不low,還是緊跟國際範的,如果覺得好,不妨用用看!

第一步:建立資源檔

第二步:添加 IHttpModule 介面實作類別

namespace PIMS_WebConsole{    public class CultureAwareHttpModule : IHttpModule    {        public void Dispose() { }        public void Init(HttpApplication context)        {            context.BeginRequest += SetCurrentCulture;        }        private void SetCurrentCulture(object sender, EventArgs args)        {            string cultureName = string.Empty;            HttpCookie cultureCookie = System.Web.HttpContext.Current.Request.Cookies[Const.CookieName_Language];            if (cultureCookie != null)            {                cultureName = cultureCookie.Value;                Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);                Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;            }        }    }}

第三步: web.config中註冊執行個體類

  <system.webServer>    <modules>      <add name="CultureAwareHttpModule" type="PIMS_WebConsole.CultureAwareHttpModule"/>    </modules>  </system.webServer>  

到此,背景多語言切換部分的代碼齊了。後面是指令碼的代碼

第四步:javascript部分的核心代碼

var __page = __page || {};__page.langSuffix = "";//語言cookie名稱__page.langcookiename = "_pims_lang";//語言類型與尾碼對照字典__page.langDictionary = { "zh-CN": "", "vi-VN": "_VN" };//選擇語言__page.lang = $.cookie(__page.langcookiename);if (!__page.lang) {    __page.lang = "zh-CN";    _setLanguageCookie(__page.lang);};//語言資訊集合__page.cultures = null;//初始化頁面語言__page.localization = function () {    kendo.culture(__page.lang);    __page.langSuffix = __page.langDictionary[__page.lang];};__page.localization();__page.setLanguageCookie = _setLanguageCookie;function _setLanguageCookie(lang) {    $.cookie(__page.langcookiename, lang, { expires: 9999 });    window.location.reload();}

第五步:view+js中語言的使用

view中直接@Resource的方式就可以了(不要跟我說怎麼我的不行呢?我不會告訴你需要去view的config中using這個命名空間的),JS中,主要是在view中定義一個Json對象,然後JS中使用此Json對象。 能一定程度的解決某些強迫症選手,對於自己代碼中各類名稱的自訂癖好。

view中樣本:

<script>var $PageLanguage = {        OK: "@Html.Raw(@Resource.OK)",        Cancel: "@Html.Raw(@Resource.Cancel)",        Create: "@Html.Raw(@Resource.Create)",        Query: "@Html.Raw(@Resource.Query)",        MachineName: "@Html.Raw(@Resource.MachineName)",        Shift: "@Html.Raw(@Resource.Shift)"    };</script>

@section Scripts {
  @Scripts.Render("~/CustomJS/test.js?v=1.0")

}

<label class="toolbar-label" for="Shift">@Resource.Shift</label>

JS中樣本:

$(function () {    alert($PageLanguage.OK);});

 

到此,你已經完成了所有的內容,可以開始你的表演了!

謝謝!

 

Asp.Net MVC 多語言(html+js共用一套資源檔)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.