我想知道现在如何使用 MVC4 来获得在 MVC3 中可能的功能,声明一些变量(和/或约束),然后在所有 cshtml 文件中可用。我用它来制作语言文件。
早些时候(在 MVC3 中)我App_Code
的解决方案中有文件夹,我将文件 Lang.cshtml 放在其中。在这个文件中(对我来说是有效的资源)我有例如
@functions{
public static readonly string G_ADD = "Dodaj";
public static readonly string G_CREATE = "Utwórz";
public static readonly string G_SAVE = "Zapisz";
public static readonly string G_EDIT = "Edytuj";
public static readonly string G_DETAILS = "Szczegóły";
public static readonly string G_DELETE = "Usuń";
public static readonly string G_SEARCH = "Szukaj";
public static readonly string G_FIRST = "<< Pierwsza";
public static readonly string G_LAST = "Ostatnia >>";
public static readonly string G_PREV = "< Poprzednia";
public static readonly string G_NEXT = "Następna >";
public static readonly string G_ACTION = "Akcja";
public static readonly string G_ID = "ID";
public static readonly string G_PAGE = "STRONA";
public static readonly string G_OF = "z";
public static readonly string G_FROM = "z";
public static readonly string G_TO = "do";
public static readonly string G_ALL = "-- Wszystkie";
public static readonly string G_RECORDS = "Rekordów:";
public static readonly string G_CHOOSE = "Wybierz:";
public static readonly string G_BACK = "Wróć";
public static readonly string G_VIEW_CHART = "Wyświetl wykres";
}
所以我可以在任何.cshtml(视图)中使用字符串,例如
@Lang.G_ADD
甚至
@Html.ActionLink(@Lang.G_ADD, "AddContract", new { id = item.CONTRACT_ID });
特别是它使我的应用程序对所有从事 i18n 工作的人来说更加用户友好。现在这个解决方案在 MVC4 中不起作用:/
可用资源解决方案不是很有效。想象一下,每次您需要编写AppName.Properties.Resources.G_ADD
.
Resources.resx 中的 XML 看起来也不错。非常。
编写任何方法来枚举资源仍然只能部分完成工作,因为它使代码膨胀,我遵循“越少代码越好”的想法。
请指出正确的方向。任何现成的解决方案也值得赞赏:)
谢谢大家!