前阵有一个很早之前的项目要进行语言的国际化,只是简单的页面的重要文字的国际化,数据库中的数据并不需要,所以就自己写了一个简单的国际化demo。
首先,考虑将需要国际化的文字放入xml中进行保存,英文保存问xxx.xml,中文保存为xxx-zh-CN.xml。其中的数据大体如下(比如中文的xml):
<?xml version="1.0" encoding="utf-8" ?>
<localizationDictionary culture="zh-CN">
<texts>
<text name="Ok" value="确定" />
<text name="AreYouSure" value="您确定吗?" />
<text name="RoleDeleteWarningMessage" value="角色 {0} 将被删除,拥有此角色的用户将取消此角色." />
<text name="DashboardNoteForMpaVersion">
<![CDATA[
查看本示例:基于<strong>多页面应用</strong>版本,基于ASP.NET MVC和<strong>jQuery</strong>.
]]>
</text>
</texts>
</localizationDictionary>
然后定义个国际化语言的类Localization,其中包含了两个list,用来存放中英文key对应的value的值。其中的方法就是根据xml读取相应的数据到对应的list中。具体代码如下:
public static IDictionary<string, string> LocalizationList_EN = new Dictionary<string, string>();
public static IDictionary<string, string> LocalizationList_ZH = new Dictionary<string, string>();
public static void GetLocalization_zh()
{
if (HttpContext.Current.Request.Cookies["Abp.Localization.CultureName"] == null)
{
HttpContext.Current.Response.Cookies.Add(new HttpCookie("Abp.Localization.CultureName", "zh-CN") { Expires = DateTime.Now.AddYears(2) });
}
if (LocalizationList_ZH == null || LocalizationList_ZH.Count == 0)
{
string path = HttpContext.Current.Server.MapPath("~/Localization/"+ LocalizationSourceName + "/"+ LocalizationSourceName + "-zh-CN.xml");
XmlDocument doc = new XmlDocument();
try
{
doc.Load(path);
//所有数据
XmlNode root = doc.SelectSingleNode("localizationDictionary");
if (root != null)
{
XmlNode mynode = root.SelectSingleNode("texts");
if (mynode != null)
{
XmlNodeList children = mynode.SelectNodes("text");
if (children != null)
{
foreach (XmlNode item in children)
{
string name = (item.Attributes["name"] == null) ? "" : item.Attributes["name"].Value;
string value = (item.Attributes["value"] == null) ? "" : item.Attributes["value"].Value;
if (string.IsNullOrEmpty(value))
{
value = item.InnerText.Replace("\n", "").Replace("\t", "").Replace("\r", "");
}
LocalizationList_ZH.Add(name, value);
}
}
}
}
}
catch
{
}
}
}
...
在basecontroller中加载语言list,大体如下:
public BaseController()
{
}
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
string langName = string.Empty;
HttpCookie langCookie = Request.Cookies["Abp.Localization.CultureName"];
if (langCookie != null)
{
langName = langCookie.Value;
}
else
{
langName = Request.UserLanguages != null && Request.UserLanguages.Length > 0 ? Request.UserLanguages[0] : string.Empty;
}
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(langName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
var lang = Thread.CurrentThread.CurrentCulture.Name;
if (lang.Equals("zh-CN"))
{
if (Localization.LocalizationList_ZH == null || Localization.LocalizationList_ZH.Count == 0)
{
Localization.GetLocalization_zh();
}
}
else
{
if (Localization.LocalizationList_EN == null || Localization.LocalizationList_EN.Count == 0 )
{
Localization.GetLocalization_en();
}
}
return base.BeginExecuteCore(callback, state);
}
最后在view中的ViewBasePage中实现L方法:
public abstract class ViewBasePage<TModel> : WebViewPage<TModel>
{
public string LanguageName { get; set; }
public ViewBasePage()
{
LanguageName = !string.IsNullOrEmpty(ConfigurationManager.AppSettings["LocalizationSourceName"]) ? ConfigurationManager.AppSettings["LocalizationSourceName"] : "YNDB";
var info = Thread.CurrentThread.CurrentCulture;
var lang = info.Name;
}
public IHtmlString L(string title, params string[] values)
{
var info = Thread.CurrentThread.CurrentCulture;
var lang = info.Name;
if (Request.Cookies["Abp.Localization.CultureName"] != null && Request.Cookies["Abp.Localization.CultureName"].Value == "zh-CN")
// if (lang == "zh-CN")
{
if (Localization.LocalizationList_ZH == null || Localization.LocalizationList_ZH.Count == 0)
{
Localization.GetLocalization_zh();
}
if (Localization.LocalizationList_ZH.ContainsKey(title))
{
if (values.Length > 0)
{
return Html.Raw(string.Format(Localization.LocalizationList_ZH[title], values));
}
else
{
return Html.Raw(Localization.LocalizationList_ZH[title]);
}
}
else
{
return Html.Raw(title);
}
}
......
页面中使用@L方法即可:
<h3>@L("AreYouSure")</h3>
<h3>@L("RoleDeleteWarningMessage","xxxx")</h3>
<h3>@L("DashboardNoteForMpaVersion")</h3>