Asp.net-Mvc

本地化的 scriptbundle 解決方案

  • May 5, 2014

您好我目前正在使用帶有 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 文化改變)。

  • “~/Scripts/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&lt;object[]&gt; getargs) {
       string virtualPath = string.Format(virtualPathMask, getargs());
       this.Include(virtualPath);
       return this;
   }

}

謝謝

康斯坦丁諾斯

沒錯,bundle 應該只在應用啟動前配置。否則,在多伺服器場景中,如果對捆綁包的請求被路由到為頁面提供服務的伺服器之外的另一台伺服器,則不會找到對捆綁資源的請求。

那有意義嗎?基本上,您的所有捆綁包都需要提前配置和定義,而不是根據每個請求動態註冊。

看看: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));
   }
}

引用自:https://stackoverflow.com/questions/10978979