在 MVC 中创建每个视图的资源文件
Creating A Per-View Resource File In MVC
我正在将本地化的 WebForms 应用程序转换为 MVC5。我见过的大多数 MVC 示例都有一个资源 - resx - 整个应用程序的文件(每种语言)。
是否可以为每个视图创建一个单独的文件?如果是这样,是否有如何引用所述文件的示例?
更新: 如果可能,我想保留未编译的资源文件。这将使我们能够即时编辑 RESX 文件,而不必每次都重新编译站点。
下面是我在 WebForms 中的程序。我实际上是在尝试在 MVC5 中重现这一点。
public string LocalizeText(Page CurrentPage, string resourceKey)
{
string localizedText = string.Empty;
// LOOK FOR LOCALIZED TEXT
String filePath = string.Empty;
if (!String.IsNullOrEmpty(CurrentPage.Request.GetFriendlyUrlFileVirtualPath()))
{
filePath = CurrentPage.Request.GetFriendlyUrlFileVirtualPath(); // FOR FRIENDLY URLS
}
else
{
filePath = CurrentPage.Request.CurrentExecutionFilePath; // FOR "UNFRIENDLY" URLS (THOSE WITH A FILE EXTENSION VISIBLE)
}
try
{
localizedText = Convert.ToString(HttpContext.GetLocalResourceObject(filePath, resourceKey, System.Globalization.CultureInfo.CurrentCulture)).Trim();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.ToString() + "<br />" + filePath);
}
return localizedText;
}
资源文件将位于 App_LocalResources 文件夹中。
是的,视图可以有一个单独的资源文件。作为一个非常简单且非常乏味的示例(对此感到抱歉:-)),请考虑以下视图模型:
using _31662592.Resources;
using System.ComponentModel.DataAnnotations;
public class HomeViewModel
{
[Display(Name = "WelcomeHeader", ResourceType = typeof(HomeResources))]
public string WelcomeHeader { get; set; }
[Display(Name = "WelcomeMessage", ResourceType = typeof(HomeResources))]
public string WelcomeMessage { get; set; }
}
在这里,我使用了支持本地化的 Display
属性。因此,就我而言,我创建的资源文件如下所示:
如您所见,ResourceType
属性 对应于资源文件的类型(即我的 HomeResources
), Name
属性 对应于您希望将 属性 绑定到的资源文件中的字符串名称。
动作没什么特别的:
public ActionResult Index()
{
return View(new HomeViewModel());
}
观点也很简单:
@model _31662592.Models.HomeViewModel
<h1>@Html.DisplayNameFor(m => m.WelcomeHeader)</h1>
<p>@Html.DisplayNameFor(m => m.WelcomeMessage)</p>
您甚至可以在视图中使用内联资源,例如:
@using _31662592.Resources
<h1>@HomeResources.WelcomeHeader</h1>
<p>@HomeResources.WelcomeMessage</p>
如果您遇到任何问题,您应该确保:
- 资源的生成操作设置为 "Embedded Resource"。
- 资源的自定义工具设置为 "PublicResXFileCodeGenerator"。
这两个选项都可以通过 right-clicking 在资源文件上并选择 Properties
来设置。然后这些选项将显示在属性可停靠 window 中。最后,如果你想引用另一个项目的资源文件,请确保它设置为 Public
而不是 internal,你可以设置 double-clicking 资源文件以正常打开它,然后更改它访问修饰符从 internal
到 public
.
我正在将本地化的 WebForms 应用程序转换为 MVC5。我见过的大多数 MVC 示例都有一个资源 - resx - 整个应用程序的文件(每种语言)。
是否可以为每个视图创建一个单独的文件?如果是这样,是否有如何引用所述文件的示例?
更新: 如果可能,我想保留未编译的资源文件。这将使我们能够即时编辑 RESX 文件,而不必每次都重新编译站点。
下面是我在 WebForms 中的程序。我实际上是在尝试在 MVC5 中重现这一点。
public string LocalizeText(Page CurrentPage, string resourceKey)
{
string localizedText = string.Empty;
// LOOK FOR LOCALIZED TEXT
String filePath = string.Empty;
if (!String.IsNullOrEmpty(CurrentPage.Request.GetFriendlyUrlFileVirtualPath()))
{
filePath = CurrentPage.Request.GetFriendlyUrlFileVirtualPath(); // FOR FRIENDLY URLS
}
else
{
filePath = CurrentPage.Request.CurrentExecutionFilePath; // FOR "UNFRIENDLY" URLS (THOSE WITH A FILE EXTENSION VISIBLE)
}
try
{
localizedText = Convert.ToString(HttpContext.GetLocalResourceObject(filePath, resourceKey, System.Globalization.CultureInfo.CurrentCulture)).Trim();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.ToString() + "<br />" + filePath);
}
return localizedText;
}
资源文件将位于 App_LocalResources 文件夹中。
是的,视图可以有一个单独的资源文件。作为一个非常简单且非常乏味的示例(对此感到抱歉:-)),请考虑以下视图模型:
using _31662592.Resources;
using System.ComponentModel.DataAnnotations;
public class HomeViewModel
{
[Display(Name = "WelcomeHeader", ResourceType = typeof(HomeResources))]
public string WelcomeHeader { get; set; }
[Display(Name = "WelcomeMessage", ResourceType = typeof(HomeResources))]
public string WelcomeMessage { get; set; }
}
在这里,我使用了支持本地化的 Display
属性。因此,就我而言,我创建的资源文件如下所示:
如您所见,ResourceType
属性 对应于资源文件的类型(即我的 HomeResources
), Name
属性 对应于您希望将 属性 绑定到的资源文件中的字符串名称。
动作没什么特别的:
public ActionResult Index()
{
return View(new HomeViewModel());
}
观点也很简单:
@model _31662592.Models.HomeViewModel
<h1>@Html.DisplayNameFor(m => m.WelcomeHeader)</h1>
<p>@Html.DisplayNameFor(m => m.WelcomeMessage)</p>
您甚至可以在视图中使用内联资源,例如:
@using _31662592.Resources
<h1>@HomeResources.WelcomeHeader</h1>
<p>@HomeResources.WelcomeMessage</p>
如果您遇到任何问题,您应该确保:
- 资源的生成操作设置为 "Embedded Resource"。
- 资源的自定义工具设置为 "PublicResXFileCodeGenerator"。
这两个选项都可以通过 right-clicking 在资源文件上并选择 Properties
来设置。然后这些选项将显示在属性可停靠 window 中。最后,如果你想引用另一个项目的资源文件,请确保它设置为 Public
而不是 internal,你可以设置 double-clicking 资源文件以正常打开它,然后更改它访问修饰符从 internal
到 public
.