DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 資源本地化——Localization
資源本地化——Localization
編輯:XML詳解     

為了支持Localization,同樣借鑒XML這一強大技術,我的本地資源化非常簡單:

  Resource.GetModule(“Menu”)[“New”]

  通過這樣一個句代碼,你便可獲取Unicode的字符了。

  當然,你要問這是怎麼來的呢?下面讓我們看一下配置文件:

<Resource>

    <config module="Product">

       <item key="Title">智睿方案框架管理系統</item>

            <item key="SubTitle">——基於面向服務的多層架構</item>

       <item key="Copyright">版權所有 ZIVSOFT 2000年-2008年</item>

            <item key="Architect">滬ICP備07505171號</item>

    </config>

    <config module="Login">

       <item key="Login">登陸</item>

            <item key="Logout">退出</item>

       <item key="UserName">用戶</item>

       <item key="PassWord">密碼</item>

       <item key="Title">登陸系統</item>

    </config>

</Resource>

  這樣的文件你可以寫多個,比如ResourceCN.xml,ResourceEN.XML等等。

為滿足回帖人要求,把最主要的Resource類帖出來了:

  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.XML;
  5 using System.IO;
  6 using System.Reflection;
  7 using System.Diagnostics;
  8 
  9 /*
 10  * Created by Lihua at 2006-11-26
 11  * http://www.zivsoft.com
 12  */
 13 
 14 namespace Zivsoft.Localization
 15 {
 16     /// <summary>
 17     /// Resource Files, Language Version Solution.
 18     /// </summary>
 19     public class Resource:IResource
 20     {
 21         /// <summary>
 22         /// string is stored in hashtable
 23         /// </summary>
 24         private static Hashtable _htStrRes;
 25         
 26         /// <summary>
 27         /// 
 28         /// </summary>
 29         private Hashtable _htData;
 30         /// <summary>
 31         /// 
 32         /// </summary>
 33         private string _module;
 34 
 35         /// <summary>
 36         /// 
 37         /// </summary>
 38         private Resource(string module)
 39         {
 40             this._module = module;
 41             this._htData = new Hashtable();
 42         }
 43 
 44         /// <summary>
 45         /// 
 46         /// </summary>
 47         static Resource()
 48         {
 49             _htStrRes = new Hashtable();
 50         }
 51 
 52         public static string CultureId
 53         {
 54             get
 55             {
 56                 var loader = new ResourceFileLoader();
 57                 return loader.GetCurrentLanguage();
 58             }
 59         }
 60 
 61         /// <summary>
 62         /// Get Resource By Module ID
 63         /// </summary>
 64         public static Resource GetModule(string module)
 65         {
 66 
 67             if (module == null || module.Trim() == "")
 68             {
 69                 throw new Exception("Resource-Instance: Module's value is null in resource files.");
 70             }
 71             //
 72             if (_htStrRes.ContainsKey(module))
 73             {
 74                 return _htStrRes[module] as Resource;
 75             }
 76             Resource strRes = new Resource(module);
 77             strRes.LoadData();
 78             _htStrRes.Add(module, strRes);
 79             return strRes;
 80         }
 81 
 82         /// <summary>
 83         /// 
 84         /// </summary>
 85         private void LoadData()
 86         {
 87             //clear
 88             this._htData.Clear();
 89             //read XML file, and store the value in hashtable
 90             string configName = null;
 91             ResourceFileLoader loader = new ResourceFileLoader();
 92             configName = loader.GetFullFilePath();
 93             if (configName == string.Empty)
 94             {
 95                 this._htData.Clear();
 96                 return;
 97             }
 98 
 99             //configuration file
100             XmlDocument xmlDoc = new XMLDocument();
101             try
102             {
103                 XMLDoc.Load(configName);
104                 XmlNode root = XMLDoc.DocumentElement;
105                 foreach (XMLNode xNode in root.ChildNodes)
106                 {
107                     //node is Config
108                     if (xNode.Name != "config")
109                     {
110                         continue;
111                     }
112 
113                     //node is module
114                     XMLAttribute attr = xNode.Attributes["module"];
115                     if (null == attr)
116                     {
117                         continue;
118                     }
119 
120                     //if it is current module
121                     if (this._module == xNode.Attributes["module"].Value)
122                     {
123                         foreach (XMLNode item in xNode.ChildNodes)
124                         {
125                             attr = item.Attributes["key"];
126                             if (null == attr || this._htData.ContainsKey(attr.Value))
127                             {
128                                 continue;
129                             }
130                             this._htData.Add(attr.Value, item.InnerText);
131                         }
132                         break;
133                     }
134                 }
135             }
136             catch (Exception e)
137             {
138                 Debug.WriteLine(e);
139                 this._htData.Clear();
140             }
141         }
142 
143         /// <summary>
144         /// 
145         /// </summary>
146         public string this[string key]
147         {
148             get
149             {
150                 string s = this._htData[key] as string;
151                 string def = "[" + this._module + "][" + key + "]";
152                 return s == null ? def : s;
153             }
154         }
155         /// <summary>
156         /// 
157         /// </summary>
158         public string GetKey(string key)
159         {
160             return this[key];
161         }
162 
163     }
164 }



XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved