Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

More robust Mapbox Streets label localization #9063

Merged
merged 1 commit into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1397,19 +1397,20 @@ - (void)styleLabelLanguageForLayersNamed:(NSArray<NSString *> *)layers

- (NSString *)bestLanguageForUser
{
NSArray *supportedLanguages = @[ @"en", @"es", @"fr", @"de", @"ru", @"zh" ];
NSArray<NSString *> *preferredLanguages = [NSLocale preferredLanguages];
NSString *bestLanguage;

for (NSString *language in preferredLanguages) {
NSString *thisLanguage = [[NSLocale localeWithLocaleIdentifier:language] objectForKey:NSLocaleLanguageCode];
if ([supportedLanguages containsObject:thisLanguage]) {
bestLanguage = thisLanguage;
break;
// https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview
NSArray *supportedLanguages = @[ @"ar", @"en", @"es", @"fr", @"de", @"pt", @"ru", @"zh", @"zh-Hans" ];
NSArray<NSString *> *preferredLanguages = [NSBundle preferredLocalizationsFromArray:supportedLanguages forPreferences:[NSLocale preferredLanguages]];
NSString *mostSpecificLanguage;

for (NSString *language in preferredLanguages)
{
if (language.length > mostSpecificLanguage.length)
{
mostSpecificLanguage = language;
}
}

return bestLanguage ?: @"en";
return mostSpecificLanguage ?: @"en";
}

- (IBAction)startWorldTour
Expand Down
2 changes: 1 addition & 1 deletion platform/macos/app/MGLVectorSource+MBXAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface MGLVectorSource (MBXAdditions)

+ (nullable NSString *)preferredMapboxStreetsLanguage;
+ (NSString *)preferredMapboxStreetsLanguage;

- (NS_DICTIONARY_OF(NSString *, NSString *) *)localizedKeysByKeyForPreferredLanguage:(nullable NSString *)preferredLanguage;

Expand Down
18 changes: 11 additions & 7 deletions platform/macos/app/MGLVectorSource+MBXAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ @implementation MGLVectorSource (MBXAdditions)
static dispatch_once_t onceToken;
static NS_SET_OF(NSString *) *mapboxStreetsLanguages;
dispatch_once(&onceToken, ^{
mapboxStreetsLanguages = [NSSet setWithObjects:@"en", @"es", @"fr", @"de", @"ru", @"zh", nil];
// https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview
mapboxStreetsLanguages = [NSSet setWithObjects:@"ar", @"de", @"en", @"es", @"fr", @"pt", @"ru", @"zh", @"zh-Hans", 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;
+ (NSString *)preferredMapboxStreetsLanguage {
NSArray<NSString *> *supportedLanguages = [MGLVectorSource mapboxStreetsLanguages].allObjects;
NSArray<NSString *> *preferredLanguages = [NSBundle preferredLocalizationsFromArray:supportedLanguages
forPreferences:[NSLocale preferredLanguages]];
NSString *mostSpecificLanguage;
for (NSString *language in preferredLanguages) {
if (language.length > mostSpecificLanguage.length) {
Copy link
Contributor

@friedbunny friedbunny May 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How robust do you think picking the longest match is? I suppose we’d have to rethink this a bit if we ever added support for, e.g., zh-Hant.

Copy link
Contributor Author

@1ec5 1ec5 May 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is sort of a hack to prioritize zh-Hans in the case where the preference is zh-*. Confusingly, the +[NSBundle preferredLocalizationsFromArray:forPreferences:] documentation claims that the method in this case returns:

typically either a single non-region-specific localization or a region-specific localization followed by a corresponding non-region-specific localization as a fallback

whereas preferredLanguages actually ends up with the value ["zh", "zh-Hans"].

Unfortunately, it is the case that, when the preference is zh-Hant or zh-Hant-*, preferredLanguages ends up being ["en"]. If we do add a zh-Hant, zh-Hant-TW will correctly match zh-Hant, however.

/ref lionheart/openradar-mirror#4831 (rdar://problem/14228383)

mostSpecificLanguage = language;
}
}
return nil;
return mostSpecificLanguage ?: @"en";
}

- (BOOL)isMapboxStreets {
Expand Down
5 changes: 2 additions & 3 deletions platform/macos/app/MapDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ - (IBAction)setLabelLanguage:(NSMenuItem *)sender {

- (void)updateLabels {
MGLStyle *style = self.mapView.style;
NSString *preferredLanguage = _isLocalizingLabels ? ([MGLVectorSource preferredMapboxStreetsLanguage] ?: @"en") : nil;
NSString *preferredLanguage = _isLocalizingLabels ? [MGLVectorSource preferredMapboxStreetsLanguage] : nil;
NSMutableDictionary *localizedKeysByKeyBySourceIdentifier = [NSMutableDictionary dictionary];
for (MGLSymbolStyleLayer *layer in style.layers) {
if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) {
Expand Down Expand Up @@ -855,8 +855,7 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
if (menuItem.tag) {
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:[NSBundle mainBundle].developmentLocalization];
NSString *preferredLanguage = [MGLVectorSource preferredMapboxStreetsLanguage];
menuItem.enabled = !!preferredLanguage;
menuItem.title = [locale displayNameForKey:NSLocaleIdentifier value:preferredLanguage ?: @"Preferred Language"];
menuItem.title = [locale displayNameForKey:NSLocaleIdentifier value:preferredLanguage];
}
return YES;
}
Expand Down