【问题标题】:Globalization unable to find appropriate resources全球化找不到合适的资源
【发布时间】:2015-08-12 11:40:17
【问题描述】:


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

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

  • Resources.resx(将包含法语)和
  • Resources.en.resx(将包含英文)。

然后我在每个文件中添加了一个具有相同名称的资源(只是为了测试)并将访问修饰符修改为 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.]

请帮忙。

【问题讨论】:

  • userLanguage 是法语吗?尝试添加 Resource.fr.resx 看看它是否有效。
  • 我自己试了一下,效果很好。您是否检查了 CultureHelper 类中的 List&lt;string&gt; _cultures 属性以包含您提供的那些文化?
  • @HariHaraChandan 我添加了 Resource.fr.resx,它是一样的。
  • @PilgerstorferFranz CultureHelper 包含我需要的文化。
  • 您是否将 buildAction 更改为 embedded ressource 并在资源文件上使用了自定义工具 PublicResXFileCodeGenerator

标签: c# asp.net asp.net-mvc globalization


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我做了一个小例子来展示你的代码应该如何工作。

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

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

    剃须刀文件

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

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

    特殊资源目录之外的 Resx 文件 MVC 中的 resx 属性

    如果您将 resx 文件添加到 MVC 项目中的任何其他文件夹或 类库中,resx 被自动设置为嵌入到 项目的输出程序集——这很好。 IDE 还分配 resx ResxCodeFileGenerator 的自定义工具,用于生成强 类型化的包装器——这很好。 生成的类是内部的 默认 - 这很糟糕。为视图创建的程序集(由 ASP.NET) 将无法使用内部类,因为它位于不同的 assembly - 在 Visual Studio 中编译的项目程序集。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2012-02-14
      相关资源
      最近更新 更多