2013-02-16 19 views
5

背景添加語言(LTR/RTL)機制來捆綁MVC 4

  • 我建立一個多語言系統
  • 我使用MVC 4個捆綁功能
  • 我有不同JavascriptsStyles文件對於從右到左(RTL)和從左到右(LTR)語言

目前我處理這種情況如下:

BundleConfig文件

//Styles for LTR 
bundles.Add(new StyleBundle("~/Content/bootstarp").Include(
       "~/Content/bootstrap.css", 
       "~/Content/CustomStyles.css")); 

// Styles for RTL 
bundles.Add(new StyleBundle("~/Content/bootstrapRTL").Include(
      "~/Content/bootstrap-rtl.css", 
      "~/Content/CustomStyles.css")); 

//Scripts for LTR 
bundles.Add(new ScriptBundle("~/scripts/bootstrap").Include(
      "~/Scripts/bootstrap.js", 
      "~/Scripts/CmsCommon.js" 
      )); 

//Scripts for RTL 
bundles.Add(new ScriptBundle("~/scripts/bootstrapRTL").Include(
      "~/Scripts/bootstrap-rtl.js", 
      "~/Scripts/CmsCommon.js" 
      )); 

實施意見:

@if (this.Culture == "he-IL") 
{ 
    @Styles.Render("~/Content/bootstrapRTL") 
} 
else 
{ 
    @Styles.Render("~/Content/bootstrap") 
} 

問題:

我想知道是否有實現它一個更好的辦法,我希望:

處理不在視圖中檢測哪種文化並在捆綁(後面的代碼)中提取正確文件的邏輯。

所以在視圖中,我將不得不做的是調用一個文件。

如果我在視圖中留下邏輯,這意味着我將不得不在每個視圖中處理它。我想避免它。

+0

BTW它引導位bootstARP;) – abatishchev 2013-02-16 10:00:31

+0

你有沒有考慮一個HTML幫手呢? – abatishchev 2013-02-16 10:02:07

+0

你的意思是使用HTML助手來獲取文件的名稱? – Silagy 2013-02-16 10:03:27

回答

3

試試自定義HTML幫助:

public static class CultureHelper 
{ 
    public static IHtmlString RenderCulture(this HtmlHelper helper, string culture) 
    { 
     string path = GetPath(culture); 
     return Styles.Render(path); 
    } 

    private static string GetPath(string culture) 
    { 
     switch (culture) 
     { 
      case "he-IL": return "~/Content/bootstarpRTL"; 
      default: return "~/Content/bootstarp"; 
     } 
    } 
} 
+0

@Ivan:謝謝! – abatishchev 2013-05-23 18:40:17

+0

嗨..不錯的代碼;)但它放在哪裏? – oCcSking 2013-05-26 14:21:50

+1

@oCcSking:謝謝:)通常在MyMvcApp \ Helpers \ CultureHelper.cs – abatishchev 2013-05-26 16:49:56

5

你並不需要使用一個開關和魔術字符串。您可以檢查一個文化是RTL與此屬性:

Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft