有没有一种方法可以告诉用户是否喜欢公制还是英制,而无需在 C# 中询问?
现在我正在做:
bool UseMetricByDefault() {
return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours >= 0;
}
这可以将美国与欧洲和亚洲区分开来,但它忽略了南美洲。
有更好的办法吗?
Right now I'm doing:
bool UseMetricByDefault() {
return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours >= 0;
}
This works for distinguishing USA from Europe and Asia, but it ignores South America.
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
RegionInfo
类在System.Globalization
命名空间中:如果您想要特定区域,可以使用以下选项之一:
Use the
RegionInfo
class in theSystem.Globalization
namespace:If you want a specific region, you can use one of the following:
部分问题在于,即使您使用他们的 RegionInfo,也不是一个区域中的每个人都使用相同的系统。在美国,科学家经常在他们的研究中使用公制,而在加拿大,这有点混合,因为我们从美国获得了很多食品包装,但我们用公制教我们的孩子(但只是从最近开始 - 我的父母仍然认为是帝国)。
话虽这么说,您可以使用 RegionInfo 类进行合理猜测。
Part of the problem is that even if you use their RegionInfo, not everyone in a region uses the same system. In the US, scientists often use metric in their research, and here in Canada, its a bit of a mix, since we get a lot of food packaging from the US but we teach our children in metric (but only since recently - my parents still think in imperial).
That being said, you can make a fair guess using the RegionInfo class.
看起来
System.Globalization 中已经有一个实现。 RegionInfo
(regionInfo.IsMetric
)。您可以使用
CultureInfo
类,它可以返回您正在运行的操作系统的Culture
- 通常采用en-US
en 格式-GB
de-DE
等文化更常用于根据操作系统的文化将应用程序从一种语言翻译为另一种语言。
这都是针对指定本地化的 - 您可能需要阅读一些有关它的教程和文章(即 此处)。
Looks like there's already an implementation in
System.Globalization.RegionInfo
(regionInfo.IsMetric
).You could use the
CultureInfo
class which can return theCulture
of the operating system you're running on - usually in the formaten-US
en-GB
de-DE
etc.Cultures are more often used for translating applications from one language to another depending on the culture of the operating system.
This is all for specified localisation - you may want to read up on some tutorials and articles regarding it (ie here).
它是
RegionInfo
类的一个属性:It's a property on the
RegionInfo
class:对于“黑暗中的刺探”来说,RegionInfo 信息已经足够了。
但实际上,像英国这样的地方也有例外。例如:
取决于您所在的一代以及您改变的态度。
因此 - 在实践中 - 提供两者,猜测一个合理的默认值,但如果它很重要 始终允许用户覆盖首选项,并确保记住这些更改。
For a "stab in the dark" the RegionInfo information is good enough.
However in practice places like the UK have exceptions. For example:
Much depends on the generation you're in and your attitude to change.
So - in practice - provide both, take a guess at a sensible default, but if it's important always allow the user to override the preference, and make sure these changes are remembered.