【发布时间】:2012-07-27 06:03:47
【问题描述】:
我有一个类库——称之为框架。在 assemblyinfo.cs 中,我有:
[assembly: NeutralResourcesLanguage("en-US")]
在 \Resources\ 目录下,我有 FrameworkResources.resx 和 FrameworkResources.fr-FR.resx
在辅助类中,我将其用作我的资源管理器:
private static ResourceManager manager = new ResourceManager("Framework.Resources.FrameworkResources", Assembly.GetExecutingAssembly());
在代码中,如果我这样做:
String message = manager.GetString("ArgumentNullExceptionMessage", CultureInfo.CurrentCulture);
我可以调试并将鼠标悬停在 CurrentCulture 上,并且它(和 UICulture)设置为 fr-FR,但这总是带回我的默认英文消息 - 而不是 fr-FR 资源文件中的法语消息。
ResourceManager 应该自动连接,还是我应该编写代码来查询当前文化,然后打开正确的文件?我是否认为资源管理器会知道获取 fr-FR 字符串是不正确的,因为那是当前的文化?
我不确定为什么这不起作用。
更新: 我有附属程序集,所以相信编译部分进展顺利。我认为问题出在我的资源管理器上。我尝试使用 ResourceSets 并像这样显式使用 ResourceManager:
ResourceManager manager =
new ResourceManager("Framework.Resources.FrameworkResources",
Assembly.GetExecutingAssembly());
Debug.WriteLine("de-DE : " + manager.GetString(resourceName, new CultureInfo("de-DE")));
Debug.WriteLine("el : " + manager.GetString(resourceName, new CultureInfo("el")));
Debug.WriteLine("es-MX : " + manager.GetString(resourceName, new CultureInfo("es-MX")));
Debug.WriteLine("fr-FR : " + manager.GetString(resourceName, new CultureInfo("fr-FR")));
Debug.WriteLine("hi : " + manager.GetString(resourceName, new CultureInfo("hi")));
Debug.WriteLine("zh-CN : " + manager.GetString(resourceName, new CultureInfo("zh-CN")));
结果如下:
de-DE : Argument '%ArgumentName%' cannot be null or empty. el : Argument '%ArgumentName%' cannot be null or empty. es-MX : Argument '%ArgumentName%' cannot be null or empty. fr-FR : Argument '%ArgumentName%' cannot be null or empty. hi : Argument '%ArgumentName%' cannot be null or empty. zh-CN : Argument '%ArgumentName%' cannot be null or empty.
因此,即使一切就绪,我的资源管理器也不会尝试查看那些附属程序集。需要明确的是,在这些程序集中,我有上面字符串的翻译版本 - 所以它肯定是从我的默认 FrameworkResources.resx 文件中读取的,而不是特定于文化的文件。
【问题讨论】:
-
据我所知,这是正确的。您是否检查过您的输出文件夹 - 是否正确创建了附属程序集?
-
是的 - 在 \bin\Debug 中,我有 Framework.dll,然后我有一个 fr-FR 子目录,在里面,我有 Framework.resources.dll - 这听起来对吗?
-
我在这方面不是特别有经验,但对我来说,是的。
-
你的代码会改变文化吗?我想知道执行线程的当前文化是否与调试器中的当前文化不同。也许在检索消息之前记录当前文化。
-
是的,它在任何地方都正确显示。我一直在逐步执行代码 - 我可以在其中观察 CultureInfo.CurrentCulture 设置为 fr-FR。为了安全起见,我还做了一个 Debug.WriteLine,这也说了同样的话。我什至指向 Resources.FrameworkResource.Culture - 并改变实际资源管理的文化。每个人都报告有 fr-FR,但我继续收到 en-US 消息。是的,我确实确认我的 fr-FR 资源文件中确实有法语消息 :-)
标签: c# localization globalization resourcemanager