diff options
Diffstat (limited to 'platform/darwin/src/MGLStyle.mm')
-rw-r--r-- | platform/darwin/src/MGLStyle.mm | 82 |
1 files changed, 18 insertions, 64 deletions
diff --git a/platform/darwin/src/MGLStyle.mm b/platform/darwin/src/MGLStyle.mm index 0162dbd354..867ac6c451 100644 --- a/platform/darwin/src/MGLStyle.mm +++ b/platform/darwin/src/MGLStyle.mm @@ -570,73 +570,27 @@ static_assert(6 == mbgl::util::default_styles::numOrderedStyles, #pragma mark Mapbox Streets source introspection -- (void)setLocalizesLabels:(BOOL)localizesLabels -{ - if (_localizesLabels != localizesLabels) { - _localizesLabels = localizesLabels; - } else { - return; - } +- (void)localizeLabelsIntoLocale:(nullable NSLocale *)locale { + NSSet<MGLVectorTileSource *> *streetsSources = + [self.sources filteredSetUsingPredicate: + [NSPredicate predicateWithBlock:^BOOL(MGLVectorTileSource * _Nullable source, NSDictionary<NSString *, id> * _Nullable bindings) { + return [source isKindOfClass:[MGLVectorTileSource class]] && [source isMapboxStreets]; + }]]; + NSSet<NSString *> *streetsSourceIdentifiers = [streetsSources valueForKey:@"identifier"]; - if (_localizesLabels) { - NSString *preferredLanguage = [MGLVectorTileSource preferredMapboxStreetsLanguage]; - NSMutableDictionary *localizedKeysByKeyBySourceIdentifier = [NSMutableDictionary dictionary]; - for (MGLSymbolStyleLayer *layer in self.layers) { - if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) { - continue; - } - - MGLVectorTileSource *source = (MGLVectorTileSource *)[self sourceWithIdentifier:layer.sourceIdentifier]; - if (![source isKindOfClass:[MGLVectorTileSource class]] || !source.mapboxStreets) { - continue; - } - - NSDictionary *localizedKeysByKey = localizedKeysByKeyBySourceIdentifier[layer.sourceIdentifier]; - if (!localizedKeysByKey) { - localizedKeysByKey = localizedKeysByKeyBySourceIdentifier[layer.sourceIdentifier] = [source localizedKeysByKeyForPreferredLanguage:preferredLanguage]; - } - - NSString *(^stringByLocalizingString)(NSString *) = ^ NSString * (NSString *string) { - NSMutableString *localizedString = string.mutableCopy; - [localizedKeysByKey enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull localizedKey, BOOL * _Nonnull stop) { - NSAssert([key isKindOfClass:[NSString class]], @"key is not a string"); - NSAssert([localizedKey isKindOfClass:[NSString class]], @"localizedKey is not a string"); - [localizedString replaceOccurrencesOfString:[NSString stringWithFormat:@"{%@}", key] - withString:[NSString stringWithFormat:@"{%@}", localizedKey] - options:0 - range:NSMakeRange(0, localizedString.length)]; - }]; - return localizedString; - }; - - if (layer.text.expressionType == NSConstantValueExpressionType) { - NSString *textField = layer.text.constantValue; - NSString *localizingString = stringByLocalizingString(textField); - if (![textField isEqualToString:localizingString]) { - MGLTextLanguage *textLanguage = [[MGLTextLanguage alloc] initWithTextLanguage:textField - updatedTextField:localizingString]; - [self.localizedLayersByIdentifier setObject:@{ textField : textLanguage } forKey:layer.identifier]; - layer.text = [NSExpression expressionForConstantValue:localizingString]; - } - } + for (MGLSymbolStyleLayer *layer in self.layers) { + if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) { + continue; + } + if (![streetsSourceIdentifiers containsObject:layer.sourceIdentifier]) { + continue; } - } else { - [self.localizedLayersByIdentifier enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, NSDictionary<NSObject *, MGLTextLanguage *> *textFields, BOOL *done) { - MGLSymbolStyleLayer *layer = (MGLSymbolStyleLayer *)[self.mapView.style layerWithIdentifier:identifier]; - - if (layer.text.expressionType == NSConstantValueExpressionType) { - NSString *textField = layer.text.constantValue; - [textFields enumerateKeysAndObjectsUsingBlock:^(NSObject *originalLanguage, MGLTextLanguage *textLanguage, BOOL *done) { - if ([textLanguage.updatedTextField isEqualToString:textField]) { - layer.text = [NSExpression expressionForConstantValue:textLanguage.originalTextField]; - } - }]; - - } - }]; - - self.localizedLayersByIdentifier = [NSMutableDictionary dictionary]; + NSExpression *text = layer.text; + NSExpression *localizedText = [text mgl_expressionLocalizedIntoLocale:locale]; + if (![localizedText isEqual:text]) { + layer.text = localizedText; + } } } |