Is there a way to find out if a user prefers metric or imperial without asking in C #?

Now I am doing:

bool UseMetricByDefault() { return TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalHours >= 0; } 

This works to distinguish the United States from Europe and Asia, but ignores South America.

Is there a better way?

+6
c # internationalization localization globalization
source share
5 answers

Use the RegionInfo class in the RegionInfo namespace:

 bool isMetric = RegionInfo.CurrentRegion.IsMetric; 

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

 // from a CultureInfo CultureInfo culture = ...; RegionInfo r = new RegionInfo(culture.Name); // from a string RegionInfo r = new RegionInfo("us"); 
+20
source share

Part of the problem is that even if you use their RegionInfo, not everyone in the region uses the same system. In the USA, scientists often use the metric in their research, but here in Canada it is a bit mixed, because we get a lot of packaging for products from the USA, but we teach our children the metric (but only recently - my parents still think in imperial )

Having said that, you can make a fair guess using the RegionInfo class.

+3
source share

It looks like an implementation already exists in regionInfo.IsMetric ( regionInfo.IsMetric ).

You can use the CultureInfo class , which can return the Culture operating system you are working on - usually in the format en-US en-GB de-DE , etc. Cultures are more often used to translate applications from one language to another, depending on the culture of the operating system.

This is all for the specified localization - you can read some tutorials and articles regarding this (i.e. here ).

+2
source share

This is a property of 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"); 
+2
source share

For a β€œstrike in the dark," RegionInfo's information is pretty good.

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

  • everything related to traffic is imperial
  • temperatures for the weather are metric, but some prefer imperial
  • equipment measurements are metric ... mainly

Much depends on the generation in which you are and on your attitude towards change.

Thus - in practice - provide both options, guess the reasonable default value, but if it is important , always allow the user to redefine preference and make sure that these changes are remembered.

0
source share

All Articles