Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 742
Silagy Avatar asked Feb 16 '13 09:02

Silagy


1 Answers

You don't need to use a switch and magic strings. You can check if a Culture is RTL with this property:

Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft
like image 151
bmegias Avatar answered Oct 20 '22 07:10

bmegias