当前系统要做成多语言,采用微软的resx资源文件,因为需要js中也要动态创建某些datagrid列,所以目前的做法是将resx文件读取之后拼接成js内容然后返回到前台进行加载,然后使用其中的语言配置,其中问题如下:
1,获取js内容的方法之前写到了APIController里了,应该写到Controller里
2,剩余的什么了,代码如下:
/// <summary>
/// 将服务器端的资源文件转换为Javascript代码,主要为了在服务端和客户端之间共享资源文件。
/// </summary>
public sealed class JavascriptResourceResult : JavaScriptResult
{
public JavascriptResourceResult(string javascriptObjectName, Type resourceType)
{
javascriptObjectName.MustNotNullAndNotWhiteSpace("javascriptObjectName");
resourceType.MustNotNull("resourceType");
//JObject retLangObj = new JObject();
声明资源管理实例对象
//ResourceManager resManager = new ResourceManager("Resources.Global", global::System.Reflection.Assembly.Load("App_GlobalResources"));
取得当前所选择的语言别名,并根据该名称创建CultureInfo数据,并依此获取多语言数据并生成json返回到前台使用
//string currentLang = HttpContext.Current.Request.Cookies["Lang"].Value;
//CultureInfo ci = CultureInfo.CreateSpecificCulture(currentLang);
//MemberInfo[] info = resourceType.GetDefaultMembers();
//FieldInfo[] fields = resourceType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
//PropertyInfo[] ss = resourceType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
//for (int i = 2; i < ss.Length; i++)
//{
// string name = ss[i].Name;
// string str2 = resManager.GetObject(name, ci).ToString();
// retLangObj[name] = str2;
// string str = resManager.GetString(name);
// string str1 = resManager.GetString("DisplayColor");
//}
var namespaceDefinitionString = GetNamespaceDefinitionString(javascriptObjectName);
var keyValueStrings = GetKeyValueString(resourceType);
this.Script = string.Format("{0};{1} = {{{2}}}", namespaceDefinitionString, javascriptObjectName, keyValueStrings);
}
private static string GetNamespaceDefinitionString(string javascriptObjectName)
{
var names = javascriptObjectName.Split('.');
var namespaces = names.Take(names.Length - 1).Select((x, index) =>
{
return String.Join(".", names.Take(index + 1).ToArray());
});
var sb = new StringBuilder();
sb.AppendFormat("var {0} = {0} || {{}}", namespaces.First());
foreach (var item in namespaces.Skip(1))
{
sb.AppendFormat("{0} = {0} || {{}}", item);
}
return sb.ToString();
}
private static string GetKeyValueString(Type resourceType)
{
var keyvalues = resourceType
.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)
.Select(x =>
{
return string.Format("{0}: '{1}'", x.Name, x.GetValue(null));
}).ToArray();
return String.Join(",", keyvalues);
}
}