ASP.NET MVC以编程方式检索本地化资源

时间:2015-09-23 18:31:45

标签: asp.net-mvc localization internationalization httphandler

我有一个HTTP处理程序,它读取ASP.NET MVC程序集中的所有资源文件并将它们转换为Javascript对象。除了1个相当大的细节之外,代码工作得很好,因为我需要能够预定义文化。我不能使用有用的Web配置的自动UI文化,而是我想使用数据库配置。一旦这段代码运行,它仍然需要我的计算机的本地文化。有没有办法设定文化?我正在考虑使用ResourceManager,但我没有这样做。

public void ProcessRequest(HttpContext context)
{
 // Check current assembly
 Assembly currentAssembly = null;

  Type appType = HttpContext.Current.ApplicationInstance.GetType();
  if (appType.Namespace == "ASP")
  {
    currentAssembly = appType.BaseType.Assembly;
  }
  else
  {
    currentAssembly = appType.Assembly;
   }

   // Get resource files in this assembly, in this cased reference by Resources namespace            
    IEnumerable<Type> resources = currentAssembly.GetTypes().Where(x => x.Namespace == "Resources");

        // Generate Javascript namespace through which each resource file will be referenced
        context.Response.ContentType = "text/javascript";
        context.Response.Write("var Resources = {};\n");

        foreach (Type resource in resources)
        {
            // For each type, add an object to the namespace
            context.Response.Write(string.Format("Resources.{0} = {{}};\n", resource.Name));

            // Get all resource keys and values for every resource file
            IDictionary<String, String> resourceKeyValues =
                resource
                .GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.GetProperty)
                .Where(x => x.PropertyType == typeof(string))
                .ToDictionary(x => x.Name, x => x.GetValue(null, null).ToString());

            // Include each key + value
            foreach (String key in resourceKeyValues.Keys)
            {
                context.Response.Write(string.Format("Resources.{0}.{1} = '{2}';\n", resource.Name, key, resourceKeyValues[key].Replace("'", "\'")));
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我设法将两种方法与请求的结果结合起来。最初我将我的代码基于this文章,并忽略了Rick Strahl的方法。由于这个问题,我更深入地调查了他的文章并且发现了this文章。

这就是我提出的:

public void ProcessRequest(HttpContext context)
    {
        // Check current assembly
        Assembly currentAssembly = null;

        Type appType = HttpContext.Current.ApplicationInstance.GetType();
        if (appType.Namespace == "ASP")
        {
            currentAssembly = appType.BaseType.Assembly;
        }
        else
        {
            currentAssembly = appType.Assembly;
        }

        ClaimsIdentity claimsIdentity = (ClaimsIdentity)HttpContext.Current.User.Identity;
        string language = claimsIdentity.FindFirst(ClaimTypes.Locality).Value;
        CultureInfo requestedCulture = new CultureInfo(language);

        // Get resource files in this assembly, in this cased reference by Resources namespace            
        IEnumerable<Type> resources = currentAssembly.GetTypes().Where(x => x.Namespace == "Resources");

        context.Response.ContentType = "text/javascript";
        context.Response.Write("var Resources = {};\n");

        foreach (Type resource in resources)
        {
            // For each type, add an object to the namespace
            context.Response.Write(string.Format("Resources.{0} = {{}};\n", resource.Name));
            Dictionary<object, object> dictionary = this.ReadResources(resource.Name, requestedCulture);

            foreach (KeyValuePair<object, object> key in dictionary)
            {
                context.Response.Write(string.Format("Resources.{0}.{1} = '{2}';\n", resource.Name, key.Key, key.Value.ToString().Replace("'", "\'")));
            }
        }
    }


    private Dictionary<object, object> ReadResources(string classKey, CultureInfo requestedCulture)
    {
        var resourceManager = new ResourceManager("Resources." + classKey, Assembly.Load("App_GlobalResources"));
        using (var resourceSet = resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true))
        {
            return resourceSet.Cast<DictionaryEntry>().ToDictionary(x => x.Key, x => resourceManager.GetObject((string)x.Key, requestedCulture));
        }
    }