diff options
author | Minh Nguyễn <mxn@1ec5.org> | 2017-05-20 19:44:52 -0700 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2017-05-23 10:09:48 -0700 |
commit | c9de6cdb6c8b4a640c10940adbd76ed508900022 (patch) | |
tree | 3509bc9021bcaf004a1b3d5656d68da0c8b4a145 /platform | |
parent | a57e9bc41093eb8f028dcab3593c3921d887bdc0 (diff) | |
download | qtlocation-mapboxgl-c9de6cdb6c8b4a640c10940adbd76ed508900022.tar.gz |
[ios, macos] More robust Streets localization
Added Arabic, Portuguese, and Simplified Chinese to the list of languages with specialized fields in the Mapbox Streets source.
Rely on NSBundle to select the most appropriate locale based on the user’s preferred languages.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/ios/app/MBXViewController.m | 21 | ||||
-rw-r--r-- | platform/macos/app/MGLVectorSource+MBXAdditions.h | 2 | ||||
-rw-r--r-- | platform/macos/app/MGLVectorSource+MBXAdditions.m | 18 | ||||
-rw-r--r-- | platform/macos/app/MapDocument.m | 5 |
4 files changed, 25 insertions, 21 deletions
diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 209998adb9..d3927374a7 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -1397,19 +1397,20 @@ typedef NS_ENUM(NSInteger, MBXSettingsMiscellaneousRows) { - (NSString *)bestLanguageForUser { - NSArray *supportedLanguages = @[ @"en", @"es", @"fr", @"de", @"ru", @"zh" ]; - NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages]; - NSString *bestLanguage; - - for (NSString *language in preferredLanguages) { - NSString *thisLanguage = [[NSLocale localeWithLocaleIdentifier:language] objectForKey:NSLocaleLanguageCode]; - if ([supportedLanguages containsObject:thisLanguage]) { - bestLanguage = thisLanguage; - break; + // https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview + NSArray *supportedLanguages = @[ @"ar", @"en", @"es", @"fr", @"de", @"pt", @"ru", @"zh", @"zh-Hans" ]; + NSArray<NSString *> *preferredLanguages = [NSBundle preferredLocalizationsFromArray:supportedLanguages forPreferences:[NSLocale preferredLanguages]]; + NSString *mostSpecificLanguage; + + for (NSString *language in preferredLanguages) + { + if (language.length > mostSpecificLanguage.length) + { + mostSpecificLanguage = language; } } - return bestLanguage ?: @"en"; + return mostSpecificLanguage ?: @"en"; } - (IBAction)startWorldTour diff --git a/platform/macos/app/MGLVectorSource+MBXAdditions.h b/platform/macos/app/MGLVectorSource+MBXAdditions.h index 312081ec51..1e25ee5a60 100644 --- a/platform/macos/app/MGLVectorSource+MBXAdditions.h +++ b/platform/macos/app/MGLVectorSource+MBXAdditions.h @@ -4,7 +4,7 @@ NS_ASSUME_NONNULL_BEGIN @interface MGLVectorSource (MBXAdditions) -+ (nullable NSString *)preferredMapboxStreetsLanguage; ++ (NSString *)preferredMapboxStreetsLanguage; - (NS_DICTIONARY_OF(NSString *, NSString *) *)localizedKeysByKeyForPreferredLanguage:(nullable NSString *)preferredLanguage; diff --git a/platform/macos/app/MGLVectorSource+MBXAdditions.m b/platform/macos/app/MGLVectorSource+MBXAdditions.m index 644b43a651..323bc74366 100644 --- a/platform/macos/app/MGLVectorSource+MBXAdditions.m +++ b/platform/macos/app/MGLVectorSource+MBXAdditions.m @@ -7,19 +7,23 @@ static dispatch_once_t onceToken; static NS_SET_OF(NSString *) *mapboxStreetsLanguages; dispatch_once(&onceToken, ^{ - mapboxStreetsLanguages = [NSSet setWithObjects:@"en", @"es", @"fr", @"de", @"ru", @"zh", nil]; + // https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview + mapboxStreetsLanguages = [NSSet setWithObjects:@"ar", @"de", @"en", @"es", @"fr", @"pt", @"ru", @"zh", @"zh-Hans", nil]; }); return mapboxStreetsLanguages; } -+ (nullable NSString *)preferredMapboxStreetsLanguage { - for (NSString *language in [NSLocale preferredLanguages]) { - NSString *languageCode = [[NSLocale localeWithLocaleIdentifier:language] objectForKey:NSLocaleLanguageCode]; - if ([[MGLVectorSource mapboxStreetsLanguages] containsObject:languageCode]) { - return languageCode; ++ (NSString *)preferredMapboxStreetsLanguage { + NSArray<NSString *> *supportedLanguages = [MGLVectorSource mapboxStreetsLanguages].allObjects; + NSArray<NSString *> *preferredLanguages = [NSBundle preferredLocalizationsFromArray:supportedLanguages + forPreferences:[NSLocale preferredLanguages]]; + NSString *mostSpecificLanguage; + for (NSString *language in preferredLanguages) { + if (language.length > mostSpecificLanguage.length) { + mostSpecificLanguage = language; } } - return nil; + return mostSpecificLanguage ?: @"en"; } - (BOOL)isMapboxStreets { diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index 59c1817f63..a30e762645 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -345,7 +345,7 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio - (void)updateLabels { MGLStyle *style = self.mapView.style; - NSString *preferredLanguage = _isLocalizingLabels ? ([MGLVectorSource preferredMapboxStreetsLanguage] ?: @"en") : nil; + NSString *preferredLanguage = _isLocalizingLabels ? [MGLVectorSource preferredMapboxStreetsLanguage] : nil; NSMutableDictionary *localizedKeysByKeyBySourceIdentifier = [NSMutableDictionary dictionary]; for (MGLSymbolStyleLayer *layer in style.layers) { if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) { @@ -855,8 +855,7 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio if (menuItem.tag) { NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[NSBundle mainBundle].developmentLocalization]; NSString *preferredLanguage = [MGLVectorSource preferredMapboxStreetsLanguage]; - menuItem.enabled = !!preferredLanguage; - menuItem.title = [locale displayNameForKey:NSLocaleIdentifier value:preferredLanguage ?: @"Preferred Language"]; + menuItem.title = [locale displayNameForKey:NSLocaleIdentifier value:preferredLanguage]; } return YES; } |