C# 向MVC4中添加语言(LTR/RTL)机制

C# 向MVC4中添加语言(LTR/RTL)机制,c#,css,asp.net-mvc,bundle,C#,Css,Asp.net Mvc,Bundle,背景 我正在建立一个多语言系统 我正在使用MVC4bundles特性 对于从右到左(RTL)和从左到右(LTR)语言,我有不同的Javascripts和style文件 目前,我按如下方式处理此场景: BundleConfig文件 //Styles for LTR bundles.Add(new StyleBundle("~/Content/bootstarp").Include( "~/Content/bootstrap.css",

背景

  • 我正在建立一个多语言系统
  • 我正在使用MVC4bundles特性
  • 对于从右到左(RTL)和从左到右(LTR)语言,我有不同的
    Javascripts
    style
    文件
目前,我按如下方式处理此场景:

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")
}
问题:

@if (this.Culture == "he-IL")
{
    @Styles.Render("~/Content/bootstrapRTL")
}
else
{
    @Styles.Render("~/Content/bootstrap")
}
我想知道是否有更好的方法来实施它,我希望:

处理检测哪个区域性的逻辑,并在bundle(代码隐藏)而不是视图中提取正确的文件

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


如果我把逻辑留在视图中,这意味着我必须在每个视图中处理它。我想避免它。

尝试自定义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";
        }
    }
}

您不需要使用开关和魔术字符串。您可以使用以下属性检查区域性是否为RTL:

Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft

顺便说一句,它的引导位bootstARP;)您考虑过使用html帮助程序吗?您的意思是使用html帮助程序获取文件名?但我想使用bundles功能。我需要在bundle中包含buth文件LTR和RTLdeceleration@oCcSking:谢谢:)通常在MyMvcApp\Helpers\CultureHelper中。cs@abatishchev请检查我的相关问题