全球化找不到合适的资源

Globalization unable to find appropriate resources


我正在尝试全球化 mvc 应用程序(法语和英语),这是我所做的:

1) 创建了一个 App_LocalResources 文件夹并添加了 2 个文件:

然后我在每个文件中添加了一个同名资源(只是为了测试)并将访问修饰符修改为Public(对于每个文件)

2) 在global.asax

中指定了法国文化
protected void Session_Start()
{
    string userLanguage = Request.UserLanguages[0];
    string cultureName = CultureHelper.GetImplementedCulture(userLanguage);
    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
    Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    HRP.App_LocalResources.Resources.Culture = Thread.CurrentThread.CurrentCulture;
}

CultureHelper是我在网上找到的一个帮手,它确定文化的正确名称(作者的学分):http://pastebin.com/JkhjJg4N

3) 添加了一个测试字符串以显示在我的视图中:

<p><span class="Title">@HRP.App_LocalResources.Resources.EmployeesTitle</span></p>

例外情况是:

[MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "HRP.App_LocalResources.Resources.resources" was correctly embedded or linked into assembly "HRP" at compile time, or that all the satellite assemblies required are loadable and fully signed.]

请帮忙。

多亏了朋友的提示,我解决了: 问题是 .resx 文件是作为内容构建的,所以我需要做的就是:

右键单击您的 ResourceFile -> 属性 -> 将 "Build Action" 属性 从 "Content" 更改为 "Embedded Resource"

我已经做了一个小例子来展示你的代码应该如何运行。

  1. 将资源的构建操作设置为嵌入式资源
  2. 输入自定义工具PublicResXFileCodeGenerator

之后您的资源将在您的代码中可用

剃须刀文件

<p>@NAMESPACE.App_LocalResources.YOUR_RESOURCE_FILE.RESOURCE_NAME</p>

我使用了另一个自定义工具 K. Scott Allen stated

Resx Files Outside Of Special Resource Directories resx properties in MVC

If you add a resx file to any other folder in an MVC project or class library, the resx is automatically set to be embedded into the project’s output assembly - this is good. The IDE also assigns the resx a custom tool of ResxCodeFileGenerator to generate a strongly typed wrapper - this is good. The generated class is internal by default – this is bad. The assembly created for a view (by ASP.NET) won’t be able to use the internal class because it is in a different assembly – the project assembly compiled in Visual Studio.

因此,如果您将资源文件添加到特殊文件夹 - 一切正常。如果您选择将它放在另一条路径上,它将失败(除非您使用自定义工具)。