有没有一种方法可以告诉用户是否喜欢公制还是英制,而无需在 C# 中询问?

发布于 2024-08-14 01:30:32 字数 205 浏览 4 评论 0原文

现在我正在做:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

装纯掩盖桑 2024-08-21 01:30:32

使用 RegionInfo 类在 System.Globalization 命名空间中:

bool isMetric = RegionInfo.CurrentRegion.IsMetric;

如果您想要特定区域,可以使用以下选项之一:

// from a CultureInfo
CultureInfo culture = ...;
RegionInfo r = new RegionInfo(culture.Name);

// from a string
RegionInfo r = new RegionInfo("us");

Use the RegionInfo class in the System.Globalization namespace:

bool isMetric = RegionInfo.CurrentRegion.IsMetric;

If you want a specific region, you can use one of the following:

// from a CultureInfo
CultureInfo culture = ...;
RegionInfo r = new RegionInfo(culture.Name);

// from a string
RegionInfo r = new RegionInfo("us");
乱世争霸 2024-08-21 01:30:32

部分问题在于,即使您使用他们的 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.

心舞飞扬 2024-08-21 01:30:32

看起来 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 the Culture of the operating system you're running on - usually in the format en-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).

妥活 2024-08-21 01:30:32

它是 RegionInfo 类的一个属性:

// en-US, en-GB, de-DE etc.
string cultureName = CultureInfo.CurrentCulture.Name;
RegionInfo regionInfo = new RegionInfo(cultureName);
Console.WriteLine("You prefer to see {0}", regionInfo.IsMetric ? "metric" : "imperial");

It's a property on the RegionInfo class:

// en-US, en-GB, de-DE etc.
string cultureName = CultureInfo.CurrentCulture.Name;
RegionInfo regionInfo = new RegionInfo(cultureName);
Console.WriteLine("You prefer to see {0}", regionInfo.IsMetric ? "metric" : "imperial");
‘画卷フ 2024-08-21 01:30:32

对于“黑暗中的刺探”来说,RegionInfo 信息已经足够了。

但实际上,像英国这样的地方也有例外。例如:

  • 任何与交通相关的事物都是
  • 英制,天气的温度都是公制的,但有些人更喜欢
  • 设备的英制尺寸是公制的......主要

取决于您所在的一代以及您改变的态度。

因此 - 在实践中 - 提供两者,猜测一个合理的默认值,但如果它很重要 始终允许用户覆盖首选项,并确保记住这些更改。

For a "stab in the dark" the RegionInfo information is good enough.

However in practice places like the UK have exceptions. For example:

  • anything traffic related is imperial
  • temperatures for weather are metric, but some people prefer imperial
  • measurements of equipment are metric... mostly

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文