添加语言(LTR/RTL)机制来捆绑MVC 4

Sil*_*agy 5 css c# asp.net-mvc bundle

背景

  • 我正在建立一个多语言系统
  • 我正在使用MVC 4捆绑功能
  • 我有从右到左(RTL)和从左到右(LTR)语言的不同JavascriptsStyles文件

目前我处理这种情况如下:

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"
            ));
Run Code Online (Sandbox Code Playgroud)

在意见中实施:

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

问题:

我想知道是否有更好的方法来实现它,我希望:

处理检测哪种文化并在捆绑中拉出正确文件的逻辑(代码隐藏)不在视图中.

所以在视图中我所要做的就是调用一个文件.

如果我在视图中保留逻辑,则意味着我将不得不在每个视图中处理它.我想避免它.

小智 5

您不需要使用开关和魔术字符串.您可以使用此属性检查Culture是否为RTL:

Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft
Run Code Online (Sandbox Code Playgroud)


aba*_*hev 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";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)