[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.
This commit is contained in:
Minh Nguyễn
2016-11-22 23:40:54 -08:00
committed by Minh Nguyễn
parent f611355912
commit 83e134eb9f
2 changed files with 90 additions and 0 deletions

View File

@@ -434,6 +434,27 @@
<action selector="toggleLayers:" target="-1" id="YdA-Mr-MHi"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="8aO-Nm-fxF"/>
<menuItem title="Labels In" id="M7v-B1-vo3">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Labels In" id="gOc-5u-4v5">
<items>
<menuItem title="Local Language" id="hTL-wF-DEs">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setLabelLanguage:" target="-1" id="Zc4-TL-Cxe"/>
</connections>
</menuItem>
<menuItem title="Preferred Language" tag="1" id="PkP-Ne-ISX">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="setLabelLanguage:" target="-1" id="7Io-iF-xf8"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="qTh-Hu-dGV"/>
</items>
</menu>
</menuItem>

View File

@@ -64,6 +64,7 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
NSUInteger _droppedPinCounter;
NSNumberFormatter *_spellOutNumberFormatter;
BOOL _isLocalizingLabels;
BOOL _showsToolTipsOnDroppedPins;
BOOL _randomizesCursorsOnDroppedPins;
BOOL _isTouringWorld;
@@ -123,10 +124,12 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
- (void)window:(NSWindow *)window willEncodeRestorableState:(NSCoder *)state {
[state encodeObject:self.mapView.styleURL forKey:@"MBXMapViewStyleURL"];
[state encodeBool:_isLocalizingLabels forKey:@"MBXLocalizeLabels"];
}
- (void)window:(NSWindow *)window didDecodeRestorableState:(NSCoder *)state {
self.mapView.styleURL = [state decodeObjectForKey:@"MBXMapViewStyleURL"];
_isLocalizingLabels = [state decodeBoolForKey:@"MBXLocalizeLabels"];
}
#pragma mark Services
@@ -325,6 +328,60 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
[self.styleLayersArrayController removeObjectsAtArrangedObjectIndexes:indices];
}
- (IBAction)setLabelLanguage:(NSMenuItem *)sender {
_isLocalizingLabels = sender.tag;
[self updateLabels];
}
- (void)updateLabels {
NSString *preferredLanguageCode = self.preferredLanguageCode;
NSString *preferredNameToken = _isLocalizingLabels ? [NSString stringWithFormat:@"{name_%@}", preferredLanguageCode] : @"{name}";
NSRegularExpression *nameTokenExpression = [NSRegularExpression regularExpressionWithPattern:@"\\{name(?:_\\w{2})?\\}" options:0 error:NULL];
for (MGLSymbolStyleLayer *layer in self.mapView.style.layers) {
if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) {
continue;
}
if ([layer.textField isKindOfClass:[MGLStyleConstantValue class]]) {
NSString *textField = [(MGLStyleConstantValue<NSString *> *)layer.textField rawValue];
textField = [nameTokenExpression stringByReplacingMatchesInString:textField
options:0
range:NSMakeRange(0, textField.length)
withTemplate:preferredNameToken];
layer.textField = [MGLStyleValue<NSString *> valueWithRawValue:textField];
} else if ([layer.textField isKindOfClass:[MGLStyleFunction class]]) {
MGLStyleFunction *function = (MGLStyleFunction<NSString *> *)layer.textField;
NSMutableDictionary *stops = function.stops.mutableCopy;
[stops enumerateKeysAndObjectsUsingBlock:^(NSNumber *zoomLevel, MGLStyleConstantValue<NSString *> *stop, BOOL *done) {
NSString *textField = stop.rawValue;
textField = [nameTokenExpression stringByReplacingMatchesInString:textField
options:0
range:NSMakeRange(0, textField.length)
withTemplate:preferredNameToken];
stops[zoomLevel] = [MGLStyleValue<NSString *> 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 <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
if (menuItem.action == @selector(deleteStyleLayers:)) {
return self.styleLayersTableView.clickedRow >= 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 <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio
#pragma mark MGLMapViewDelegate methods
- (void)mapView:(MGLMapView *)mapView didFinishLoadingStyle:(MGLStyle *)style {
[self updateLabels];
}
- (BOOL)mapView:(MGLMapView *)mapView annotationCanShowCallout:(id <MGLAnnotation>)annotation {
return YES;
}