diff --git a/platform/macos/app/MGLVectorSource+MBXAdditions.h b/platform/macos/app/MGLVectorSource+MBXAdditions.h new file mode 100644 index 000000000..312081ec5 --- /dev/null +++ b/platform/macos/app/MGLVectorSource+MBXAdditions.h @@ -0,0 +1,15 @@ +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface MGLVectorSource (MBXAdditions) + ++ (nullable NSString *)preferredMapboxStreetsLanguage; + +- (NS_DICTIONARY_OF(NSString *, NSString *) *)localizedKeysByKeyForPreferredLanguage:(nullable NSString *)preferredLanguage; + +@property (nonatomic, readonly, getter=isMapboxStreets) BOOL mapboxStreets; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/macos/app/MGLVectorSource+MBXAdditions.m b/platform/macos/app/MGLVectorSource+MBXAdditions.m new file mode 100644 index 000000000..f59b72aa9 --- /dev/null +++ b/platform/macos/app/MGLVectorSource+MBXAdditions.m @@ -0,0 +1,49 @@ +#import "MGLVectorSource+MBXAdditions.h" + +@implementation MGLVectorSource (MBXAdditions) + ++ (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, ^{ + mapboxStreetsLanguages = [NSSet setWithObjects:@"en", @"es", @"fr", @"de", @"ru", @"zh", 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; + } + } + return nil; +} + +- (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 diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index 2c3161077..705ebb8a4 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -4,6 +4,8 @@ #import "LimeGreenStyleLayer.h" #import "DroppedPinAnnotation.h" +#import "MGLVectorSource+MBXAdditions.h" + #import static NSString * const MGLDroppedPinAnnotationImageIdentifier = @"dropped"; @@ -331,36 +333,50 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id *)layer.textField rawValue]; - textField = [nameTokenExpression stringByReplacingMatchesInString:textField - options:0 - range:NSMakeRange(0, textField.length) - withTemplate:preferredNameToken]; - layer.textField = [MGLStyleValue valueWithRawValue:textField]; + layer.textField = [MGLStyleValue valueWithRawValue:stringByLocalizingString(textField)]; } else if ([layer.textField isKindOfClass:[MGLStyleFunction class]]) { MGLStyleFunction *function = (MGLStyleFunction *)layer.textField; NSMutableDictionary *stops = function.stops.mutableCopy; [stops enumerateKeysAndObjectsUsingBlock:^(NSNumber *zoomLevel, MGLStyleConstantValue *stop, BOOL *done) { NSString *textField = stop.rawValue; - textField = [nameTokenExpression stringByReplacingMatchesInString:textField - options:0 - range:NSMakeRange(0, textField.length) - withTemplate:preferredNameToken]; - stops[zoomLevel] = [MGLStyleValue valueWithRawValue:textField]; + stops[zoomLevel] = [MGLStyleValue valueWithRawValue:stringByLocalizingString(textField)]; }]; function.stops = stops; layer.textField = function; @@ -368,21 +384,6 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id