asp.net mvc 国际化(2) 解决问题
解决上一篇提出的剩下的问题
前端国际化. 通过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 == keyselect 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));
public static class GetEnumDescribeExt { private static ConcurrentDictionary<string, string> dic = new ConcurrentDictionary<string, string>(); public static string GetEnumDescribe(this Enum value) { bool nameInstend = true; Type type = value.GetType(); string name = Enum.GetName(type, value); var key = type.FullName + "." + name; if (dic.ContainsKey(key)) return dic[key]; else { if (name == null) { return null; } FieldInfo field = type.GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null && nameInstend == true) { return name; } string result = attribute == null ? null : attribute.Description; dic[key] = result; return result; } } }
数据库文本等的国际化
新建资源表SELECT [crId]
,[CultureResourceCode],[CultureName],[ResourceName],[Status]FROM [Boss].[dbo].[CultureResource]
GOcrid, 自增字段, 主键CultureResourceCode: 资源键CultureName: asp.net程序当前线程的语言区域值ResourceName: 资源值status: 状态是否启用(0,1)在需要用到文本的各个字段都加上CultureResourceCode外键即可
本人在长沙, 有工作可以加我QQ4658276