本地化的scriptbundle解决方案

时间:2012-06-11 11:11:33

标签: asp.net-mvc localization bundling-and-minification asp.net-optimization

您好我正在使用带有System.Web.Optimization的asp.net MVC 4 rc。由于我的网站需要根据用户偏好进行本地化,因此我正在使用jquery.globalize插件。

我非常希望继承ScriptBundle类,并根据System.Threading.Thread.CurrentThread.CurrentUICulture确定要捆绑的文件。这看起来像这样:

bundles.Add(new LocalizedScriptBundle("~/bundles/jqueryglobal")
                             .Include("~/Scripts/jquery.globalize/globalize.js")
                             .Include("~/Scripts/jquery.globalize/cultures/globalize.culture.{0}.js", 
                                       () => new object[] { Thread.CurrentThread.CurrentUICulture })
                    ));

例如,如果ui文化是“en-GB”,我希望拾取以下文件(当然会缩小,如果可能的话也会缓存,直到脚本文件或currentui文化发生变化)。

  • “〜/脚本/ jquery.globalize / globalize.js”
  • “〜/ Scripts / jquery.globalize / globalize-en-GB.js”< - 如果此文件在服务器文件系统上不存在,则回退到globalize-en.js。

我尝试使用类似下面的内容重载Include方法,但这不会起作用,因为它不是根据请求进行评估,而是在应用程序启动时进行评估。

public class LocalizedScriptBundle : ScriptBundle
{
    public LocalizedScriptBundle(string virtualPath)
        : base(virtualPath) { 
    }

    public Bundle Include(string virtualPathMask, Func<object[]> getargs) {
        string virtualPath = string.Format(virtualPathMask, getargs());
        this.Include(virtualPath);
        return this;
    }

}

谢谢

康斯坦丁

2 个答案:

答案 0 :(得分:9)

这是正确的,只应在app app start之前配置bundle。否则,在多服务器方案中,如果捆绑包的请求被路由到服务页面的服务器以外的其他服务器,则无法找到捆绑包资源的请求。

这有意义吗?基本上所有的捆绑包都需要提前配置和定义,而不是基于每个请求动态注册。

答案 1 :(得分:1)

看看:https://stackoverflow.com/questions/18509506/search-and-replace-in-javascript-before-bundling

我按照我的需要编码:

public class MultiLanguageBundler : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse bundle)
    {
        var content = new StringBuilder();
        var uicult = Thread.CurrentThread.CurrentUICulture.ToString();

        var localizedstrings = GetFileFullPath(uicult);
        if (!File.Exists(localizedstrings))
        {
            localizedstrings = GetFileFullPath(string.Empty);
        }

        using (var fs = new FileStream(localizedstrings, FileMode.Open, FileAccess.Read))
        {
            var m_streamReader = new StreamReader(fs);
            var str = m_streamReader.ReadToEnd();

            content.Append(str);
            content.AppendLine();
        }

        foreach (var file in bundle.Files)
        {
            var f = file.VirtualFile.Name ?? "";

            if (!f.Contains("localizedstrings"))
            {
                using (var reader = new StreamReader(VirtualPathProvider.OpenFile(file.VirtualFile.VirtualPath)))
                {
                    content.Append(reader.ReadToEnd());
                    content.AppendLine();
                }
            }
        }

        bundle.ContentType = "text/javascript";
        bundle.Content = content.ToString();
    }

    private string GetFileFullPath(string uicult)
    {
        if (uicult.StartsWith("en"))
            uicult = string.Empty;
        else if (!string.IsNullOrEmpty(uicult))
            uicult = ("." + uicult);

        return Kit.ToAbsolutePath(string.Format("~/Scripts/locale/localizedstrings{0}.js", uicult));
    }
}
相关问题