From 758ea28de61719fb7983e83f8f03492855fd1006 Mon Sep 17 00:00:00 2001 From: Nic Hubbard Date: Sat, 10 Jul 2021 16:39:40 -0700 Subject: [PATCH] Fixes for locales and using decimals or commas as seperator. (#121) --- platform/ios/platform/ios/src/MGLScaleBar.mm | 22 ++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/platform/ios/platform/ios/src/MGLScaleBar.mm b/platform/ios/platform/ios/src/MGLScaleBar.mm index f5aed5fd8..2aeb479ed 100644 --- a/platform/ios/platform/ios/src/MGLScaleBar.mm +++ b/platform/ios/platform/ios/src/MGLScaleBar.mm @@ -326,8 +326,26 @@ static const CGFloat MGLScaleBarMinimumBarWidth = 30.0; // Arbitrary _usesMetricSystem = usesMetricSystem; - // Set the distance formatter locale using Germany for metric and United States for imperial. - self.formatter.numberFormatter.locale = usesMetricSystem ? [NSLocale localeWithLocaleIdentifier:@"de_DE"] : [NSLocale localeWithLocaleIdentifier:@"en_US"]; + // Find the current locale for the system, then check if we need to override this. E.g. Locale is in the United States but we want to see metric. + NSLocale *locale = [NSLocale currentLocale]; + BOOL currentLocaleUsesMetric = [[locale objectForKey:NSLocaleUsesMetricSystem] boolValue]; + if (currentLocaleUsesMetric && !usesMetricSystem) { + + // Our current locale is metric, but for some reason we don't want to show metric here, so we need to force a locale that isn't metric. + self.formatter.numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"]; + + } else if (!currentLocaleUsesMetric && usesMetricSystem) { + + // Our current locale is not metric, but we want to use the metric system for the scale bar. + // Use Canada as the identifier as they use a period as a decimal seperator + self.formatter.numberFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_CA"]; + + } else if ((currentLocaleUsesMetric && usesMetricSystem) || (!currentLocaleUsesMetric && !usesMetricSystem)) { + + // Fallback to the system locale. + self.formatter.numberFormatter.locale = locale; + + } [self resetLabelImageCache]; [self updateVisibility];