From 83e134eb9f2a8737d2ce008cd7564b4df0777c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguye=CC=82=CC=83n?= Date: Tue, 22 Nov 2016 23:40:54 -0800 Subject: [PATCH] [macos] Added option to localize labels Added a setting to macosapp to change all symbol layers in the current style to use the preferred language if available. If the setting is off, change all symbol layers to use local languages. --- platform/macos/app/Base.lproj/MainMenu.xib | 21 +++++++ platform/macos/app/MapDocument.m | 69 ++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/platform/macos/app/Base.lproj/MainMenu.xib b/platform/macos/app/Base.lproj/MainMenu.xib index 4efb8d4c4..cb9905d4a 100644 --- a/platform/macos/app/Base.lproj/MainMenu.xib +++ b/platform/macos/app/Base.lproj/MainMenu.xib @@ -434,6 +434,27 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index c05a85f96..d2e6a0f81 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -64,6 +64,7 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id ) *MBXFlattenedShapes(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]; + } 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]; + }]; + function.stops = stops; + layer.textField = function; + } + } +} + +- (NSString *)preferredLanguageCode { + // Languages supported by Mapbox Streets v10. + NSSet *supportedLanguages = [NSSet setWithObjects:@"en", @"es", @"fr", @"de", @"ru", @"zh", nil]; + NSArray *preferredLanguages = [NSLocale preferredLanguages]; + + for (NSString *language in preferredLanguages) { + NSString *languageCode = [[NSLocale localeWithLocaleIdentifier:language] objectForKey:NSLocaleLanguageCode]; + if ([supportedLanguages containsObject:languageCode]) { + return languageCode; + } + } + + return @"en"; +} + - (void)applyPendingState { if (_inheritedStyleURL) { self.mapView.styleURL = _inheritedStyleURL; @@ -736,6 +793,14 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id = 0 || self.styleLayersTableView.selectedRow >= 0; } + if (menuItem.action == @selector(setLabelLanguage:)) { + menuItem.state = menuItem.tag == _isLocalizingLabels ? NSOnState: NSOffState; + if (menuItem.tag) { + NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[NSBundle mainBundle].developmentLocalization]; + menuItem.title = [locale displayNameForKey:NSLocaleIdentifier value:self.preferredLanguageCode]; + } + return YES; + } if (menuItem.action == @selector(manipulateStyle:)) { return YES; } @@ -922,6 +987,10 @@ NS_ARRAY_OF(id ) *MBXFlattenedShapes(NS_ARRAY_OF(id )annotation { return YES; }