This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[macos] Refactored label localization
Localize only Mapbox Streets v6–v7, and do so more systematically than before.
- Loading branch information
Showing
4 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#import <Mapbox/Mapbox.h> | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters