Add language (LTR/RTL) mechanism to bundles MVC 4

asp.net-mvc, bundle, c#, css

Solution

Try custom 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";
        }
    }
}

Problem

Background - I am building a multilingual system - I am using MVC 4 bundles feature - I have different `Javascripts` and `Styles` files for Right-To-Left (RTL) and Left-To-Right (LTR) languages Currently i handle this scenario as follow: 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" )); ``` Implementation in the views: ``` @if (this.Culture == "he-IL") { @Styles.Render("~/Content/bootstrapRTL") } else { @Styles.Render("~/Content/bootstrap") } ``` The question: I was wondering if there is a better way to implement it, i was hoping for: Handle the logic of detecting which culture and pull the right file in the bundles (Code behind) not in the Views. So in the views all i will have to do is calling to one file. If i am leaving the logic in the views its means that i will have to handle it in each view. I want to avoid it.

Original source

Related problems