【问题标题】:RegionInfo: DisplayName translated by CurrentUICultureRegionInfo:由 CurrentUICulture 翻译的 DisplayName
【发布时间】:2016-12-05 09:19:32
【问题描述】:

有没有办法让自定义 CultureInfo 翻译 RegionInfo?

我尝试了以下方法,但它不起作用:

/* Change culture of current thread to german*/
CultureInfo de = new CultureInfo("de");
Thread.CurrentThread.CurrentCulture = de;
Thread.CurrentThread.CurrentUICulture = de;
RegionInfo regionInfoDe = new RegionInfo("de");
regionInfoDe.DisplayName.Should().Be("Deutschland");

/* Change culture of current thread to englisch */
CultureInfo en = new CultureInfo("en");
Thread.CurrentThread.CurrentCulture = en;
Thread.CurrentThread.CurrentUICulture = en;
RegionInfo regionInfoEn = new RegionInfo("de");
regionInfoEn.DisplayName.Should().Be("Germany");

如果当前文化是德语,我想从 RegionInfo("de") 获取“Deutschland”。如果当前的文化是英语,它应该是“德国”。

【问题讨论】:

  • 如果CultureInfo.CurrentCulture 是“de”和DisplayName 不同,你不能使用NativeName 吗?
  • 我们讨论了这个解决方案,但是如果应用程序语言是英语,应该有“德国”而不是“德国”。

标签: c# localization translation regioninfo


【解决方案1】:

您不能为此使用 RegionInfo - 此类仅允许您以 .NET 安装的语言(通常为英语)或本地名称(以所选区域的语言)获取名称。

您应该为此使用资源文件(例如 Res.resx: "de" "Germany"; Res.de.resx: "de" "Deutschland"; Res.pl.resx: "de" "Niemcy".. .) 然后,在代码中,Res.de 的值将从与您的 CurrentThread.CurrentUICulture 对应的 resx 中获取。

【讨论】:

  • 这正是我最终的结果。
猜你喜欢
  • 2016-07-11
  • 2012-02-22
  • 2010-09-22
  • 2017-04-15
  • 2018-09-02
  • 2019-11-18
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多