我的应用程序中有managed to localize the view pages但有些母版页包含一些字符串。
主页中包含的字符串似乎必须添加到每个页面的资源文件中。这看起来很可怕。如何在母版页中优雅地本地化字符串?
答案 0 :(得分:3)
如果您不想搞乱访问修饰符,您可以帮助简化您必须编写的代码以访问资源文件,例如:
public static class LocalizationHelper
{
public static string Localize(this HtmlHelper helper, string key)
{
var resourceObject = helper.ViewContext.HttpContext.GetGlobalResourceObject("NameOfResourceFileClass", key);
if (resourceObject == null)
{
// i don't recommend throwing the Exception class, I'd define my own Exception type here
throw new Exception(String.Format("Resource key '{1}' could not be found in Resource class '{0}'","NameOfResourceFileClass", key));
}
return resourceObject.ToString();
}
}
然后在你的.master ......
<%= Html.Localize("NameOfResourceKey") %>
答案 1 :(得分:2)
您应该使用全局资源文件。
App_GlobalResources
asp.net文件夹Public
My.Resources.Resource.MyText
(VB语法)
从主页的源代码访问资源:
<asp:Literal ID="Literal2" runat="server" Text="<%$ Resources:ResourcesFileName, ResourcesName%>" />