【问题标题】:Add language (LTR/RTL) mechanism to bundles MVC 4将语言 (LTR/RTL) 机制添加到捆绑 MVC 4
【发布时间】:2013-02-01 06:19:28
【问题描述】:

背景

  • 我正在构建一个多语言系统
  • 我正在使用 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"
            ));

视图中的实现:

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

问题:

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

处理检测哪种文化的逻辑并将正确的文件拉到包中(代码隐藏)而不是在视图中。

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

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

【问题讨论】:

  • 顺便说一句,它的 bootstRAP 位 bootstARP ;)
  • 您是否考虑过使用 html 助手?
  • 你的意思是使用 HTML helper 来获取文件名?
  • 但我想使用捆绑功能。还有一些我需要在捆绑减速中包含 bth 文件 LTR 和 RTL

标签: c# css asp.net-mvc bundle


【解决方案1】:

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

Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft

【讨论】:

    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 2017-08-14
    • 2016-04-10
    • 1970-01-01
    • 2021-01-09
    • 2018-04-25
    • 1970-01-01
    • 2020-10-27
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多