我在根据 CultureInfo 对象检索正确的翻译文件 (.resx) 时遇到问题。所以我有一个翻译解决方案,其中有四个 .resx 文件(应用程序的意大利语翻译、荷兰语、德语和英语):
在我的应用程序中,我引用了 Qit.WindowsStrings dll,然后我可以在我的应用程序中使用翻译,如下所示:
lblAddress1.Text = Qit.WindowsStrings.WinStrings.Address1;
问题是我总是检索默认的 .resx 文件,因此翻译始终是英语。
如果我使用以下代码:
string language = "Nederlands";
switch (language)
{
case "English GB":
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
break;
case "English US":
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
break;
case "Deutsch":
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
break;
case "Italiano":
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("it-IT");
break;
case "Nederlands":
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL");
break;
}
string test = Qit.WindowsStrings.WinStrings.AppointmentConflictNotAllowed;
string test2 = Qit.WindowsStrings.Utils.GetString("AppointmentConflictNotAllowed", System.Threading.Thread.CurrentThread.CurrentUICulture);
..CurrentUICulture 是荷兰语,但我仍然得到英语 .resx 文件和英语翻译。
显然 ResourceManager 类总是检索英文 .resx 文件:
public static string GetString(string code, CultureInfo cultureInfo)
{
string result = string.Empty;
try
{
ResourceManager resourceManager = new ResourceManager(typeof(WinStrings));
result = ((cultureInfo == null) ? resourceManager.GetString(code) : resourceManager.GetString(code, cultureInfo));
}
catch
{
}
return result;
}
因此,在我的应用程序解决方案中的构建调试文件夹中,意大利语、荷兰语和德语翻译 dll 文件位于单独的文件夹中:
.. 而英文版 Qit.WindowsStrings.resources.dll 位于主文件夹中。 也许这与这个问题有关?奇怪的是,它过去总是工作得很好。一定是有什么小事造成了这种情况。 有人知道这里出了什么问题吗?
谢谢你,null 博士。我现在已经尝试了以下方法(就像你说的):
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string path2 = Path.Combine(path , "nl");
string[] dll = Directory.GetFiles(path2, "*.resources.dll");
Assembly loadedAssembly = Assembly.LoadFile(dll[0]);
ResourceManager resourceManager1 = new ResourceManager(typeof(WinStrings).ToString(), loadedAssembly);
string test4 = resourceManager1.GetString("AppointmentConflictNotAllowed", System.Threading.Thread.CurrentThread.CurrentUICulture);
这样我就可以“基于”Dutch Qit.WindowsStrings.resources.dll 加载资源管理器。
但随后我收到以下错误消息:
System.Resources.MissingManifestResourceException:'找不到任何 适合特定文化或中立的资源 文化。确保“Qit.WindowsStrings.WinStrings.resources”是 正确嵌入或链接到程序集中 编译时的“Qit.WindowsStrings.resources”,或者所有的 所需的卫星程序集可加载且已完全签名。'
..在这行代码中:
string test4 = resourceManager1.GetString("AppointmentConflictNotAllowed", System.Threading.Thread.CurrentThread.CurrentUICulture);
有人知道导致此错误的原因吗?