asp.net mvc 國際化(2) 解決問題

來源:互聯網
上載者:User

解決上一篇提出的剩下的問題

 

前端國際化. 通過js語言套件解決.

準備好js語言套件. 在模板頁中使用如下代碼

<script src='http://www.cnblogs.com/Scripts/grid.locale-<%=System.Threading.Thread.CurrentThread.CurrentCulture.Name %>.js' type="text/javascript"></script>

注意語言套件的命名
後台輸出的提示言等的國際化.(比如說”該使用者名稱不存在”)

簡單方法: 將該語句直接寫入全域資源檔再用資源類進行訪問

複雜一點: 將語句匯總成xml檔案, 封裝起來提供使用.

1. 增加全域資源檔

2. 在全域資源檔夾下增加XML檔案如下

<?xml version="1.0" encoding="utf-8" ?>
<root>
    <Text key="UsernameExist" zh-CN="使用者名稱已經存在" en-US="username is already exist!"></Text>
</root>

3. 提供訪問類

//枚舉類
        public static class ResponseKey {
            public static const string UsernameExist = "UsernameExist";
        }
 
        private static XElement root = XElement.Parse(ResponseIntel.ResponseIntelXml);
        private static string CacheKey = "ResponseIntelXML";
        public static string Lang(string key) {
            if (HttpContext.Current.Cache.Get(CacheKey + key) == null)
            {
                var nodes = (from el in root.Elements("Text")
                             where el.Attribute("key").Value == key
                             select el).First();
                string ret = nodes.Attribute(System.Threading.Thread.CurrentThread.CurrentCulture.Name).Value;
                //System.Web.Caching.CacheDependency dependency = new System.Web.Caching.CacheDependency(
                //    HttpContext.Current.Server.MapPath("~/App_GlobalResources/ResponseIntelXml.xml"));
                HttpContext.Current.Cache[CacheKey + key] = ret;
                return ret;
            }
            else
                return (string)HttpContext.Current.Cache.Get(CacheKey + key);
        }

4. 訪問

         Common.RespAlertIntel_.Lang(Common.RespAlertIntel_.ResponseKey.UsernameExist)

ASP.NET MVC自訂驗證提示資訊國際化

1. 建立一個全域資源檔. 增加一個key/value為  LoginModel_Username:’使用者名稱不可為空’

2. 在該model的特性代入類似如下

[Required(ErrorMessageResourceType=typeof(資源類名), ErrorMessageResourceName="LoginModel_Username")]
        [DisplayName("使用者名稱")]
        public string UserName { get; set; }

狀態枚舉的國際化.(比如說”正常”,”停止”等狀態)

使用了Attribute特性

//屬性類別和枚舉
[AttributeUsage(AttributeTargets.All,AllowMultiple=true)]
        public class LanguageAttribute : Attribute {
            public LanguageAttribute(string languageKey, string languageValue)
            {
                LanguageKey = languageKey; LanguageValue = languageValue;
            }
            public string LanguageKey { get; set; }
            public string LanguageValue { get; set; }
        }
        public enum State { 
            [Language("zh-CN","錯誤了")]
            [Language("en-US", "Error!!!")]
            Error=0,
            [Language("zh-CN", "成功了也")]
            [Language("en-US", "YES!!!")]
            OK=1
        }

       //核心處理方法
       public string getStr(State state) {
            var attr = state.GetType().GetField(state.ToString()).GetCustomAttributes(false);
            string ret = "";
            foreach (var item in attr)
            {
                var lang=((LanguageAttribute)item);
                if (System.Threading.Thread.CurrentThread.CurrentCulture.Name.ToLower() == lang.LanguageKey.ToLower()) {
                    ret = lang.LanguageValue;
                    break;
                }
            }
            return ret;
        }

測試.

            Response.Write("多語言<br>現在是中文<br>");
            Response.Write(getStr(State.Error)); Response.Write(getStr(State.OK));
            Response.Write("多語言<br>現在是Ying文<br>");
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
            Response.Write(getStr(State.Error)); Response.Write(getStr(State.OK));

 

資料庫文本等的國際化

建立資源表

SELECT [crId]
      ,[CultureResourceCode]
      ,[CultureName]
      ,[ResourceName]
      ,[Status]
  FROM [Boss].[dbo].[CultureResource]
GO

crid, 自增欄位, 主鍵CultureResourceCode: 資源鍵CultureName: asp.net程式當前線程的語言地區值ResourceName: 資源值status: 狀態是否啟用(0,1) 在需要用到文本的各個欄位都加上CultureResourceCode外鍵即可
相關文章

聯繫我們

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