2013-02-16 9 views
5

SfondoAdd lingua (LTR/RTL) meccanismo per bundle MVC 4

  • Sto costruendo un sistema multilingue
  • Sto usando MVC 4 fasci dispongono
  • Ho diversi Javascripts e Styles file per le lingue Right-To-Left (RTL) e Left-To-Right (LTR)

Attualmente gestisco questo scenario come segue:

BundleConfig file

//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" 
      )); 

Attuazione della vista:

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

La domanda:

Mi chiedevo se esiste un modo migliore per attuarlo , speravo in:

Gestire la logica di rilevamento di quale cultura e tirare il file corretto nei pacchetti (Code behind) non nelle Visualizzazioni.

Quindi nelle visualizzazioni tutto ciò che dovrò fare è chiamare su un file.

Se sto lasciando la logica nelle viste, significa che dovrò gestirlo in ogni vista. Voglio evitarlo.

+0

btw è' Bootstrap bit bootstARP;) – abatishchev

+0

Avete considerato un aiutante html, invece? – abatishchev

+0

Intendi usare l'helper HTML per ottenere il nome del file? – Silagy

risposta

3

Prova personalizzato HTML helper:

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: Grazie! – abatishchev

+0

Ciao .. Bel codice;) ma dove metterlo? – oCcSking

+1

@oCcSking: Grazie :) Di solito in MyMvcApp \ Helpers \ CultureHelper.cs – abatishchev

5

Non è necessario utilizzare un interruttore e archi magici. È possibile verificare se una cultura è RTL con questa proprietà:

Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft