diff options
author | Fabian Guerra Soto <fabian.guerra@mapbox.com> | 2017-08-01 16:14:41 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-01 16:14:41 -0400 |
commit | afd4df9dbcdff08654a09e95fdae236061aaa4bd (patch) | |
tree | 31ee16d763c44f28b8556127ad729fc42a7a1e32 /platform/darwin/src/MGLVectorSource+MGLAdditions.m | |
parent | a523b710aceccc992c5aa193acbcd4bf4c93a62d (diff) | |
download | qtlocation-mapboxgl-afd4df9dbcdff08654a09e95fdae236061aaa4bd.tar.gz |
[ios] adapt Mapbox Streets–sourced layers for user preferred language (#9582)
* [ios] Update label localization
* [ios, macos] Move MGLVectorSource+MBXAdditions.h to darwin.
* [ios, macos] Adapt Mapbox Streets to the user preferred language.
* [ios, macos] Clarify style localization documentation.
* [ios, macos] Update localization examples
* [ios, macos] Add style language reset to original.
* [ios, macos] Update changelogs.
* [ios, macos] Rename Vector Source Additions to MGL standard.
* [ios, macos] Add suport for stop localization.
Diffstat (limited to 'platform/darwin/src/MGLVectorSource+MGLAdditions.m')
-rw-r--r-- | platform/darwin/src/MGLVectorSource+MGLAdditions.m | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/platform/darwin/src/MGLVectorSource+MGLAdditions.m b/platform/darwin/src/MGLVectorSource+MGLAdditions.m new file mode 100644 index 0000000000..a305388117 --- /dev/null +++ b/platform/darwin/src/MGLVectorSource+MGLAdditions.m @@ -0,0 +1,53 @@ +#import "MGLVectorSource+MGLAdditions.h" + +@implementation MGLVectorSource (MGLAdditions) + ++ (NS_SET_OF(NSString *) *)mapboxStreetsLanguages { + // https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview + static dispatch_once_t onceToken; + static NS_SET_OF(NSString *) *mapboxStreetsLanguages; + dispatch_once(&onceToken, ^{ + // 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; +} + ++ (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 mostSpecificLanguage ?: @"en"; +} + +- (BOOL)isMapboxStreets { + NSURL *url = self.configurationURL; + if (![url.scheme isEqualToString:@"mapbox"]) { + return NO; + } + NSArray *identifiers = [url.host componentsSeparatedByString:@","]; + return [identifiers containsObject:@"mapbox.mapbox-streets-v7"] || [identifiers containsObject:@"mapbox.mapbox-streets-v6"]; +} + +- (NS_DICTIONARY_OF(NSString *, NSString *) *)localizedKeysByKeyForPreferredLanguage:(nullable NSString *)preferredLanguage { + if (!self.mapboxStreets) { + return @{}; + } + + // Replace {name} and {name_*} with the matching localized name tag. + NSString *localizedKey = preferredLanguage ? [NSString stringWithFormat:@"name_%@", preferredLanguage] : @"name"; + NSMutableDictionary *localizedKeysByKey = [NSMutableDictionary dictionaryWithObject:localizedKey forKey:@"name"]; + for (NSString *languageCode in [MGLVectorSource mapboxStreetsLanguages]) { + NSString *key = [NSString stringWithFormat:@"name_%@", languageCode]; + localizedKeysByKey[key] = localizedKey; + } + return localizedKeysByKey; +} + +@end |