Skip to content

Instantly share code, notes, and snippets.

@1ec5 1ec5/v8.patch
Created Nov 26, 2018

Embed
What would you like to do?
diff --git a/platform/darwin/src/MGLVectorTileSource.mm b/platform/darwin/src/MGLVectorTileSource.mm
index a3241d3bce..b1bf20aefe 100644
--- a/platform/darwin/src/MGLVectorTileSource.mm
+++ b/platform/darwin/src/MGLVectorTileSource.mm
@@ -79,11 +79,11 @@
An array of locale codes with dedicated name fields in the Mapbox Streets
source.
- https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview
+ https://www.mapbox.com/vector-tiles/mapbox-streets-v8/#overview
*/
static NSArray * const MGLMapboxStreetsLanguages = @[
@"ar", @"de", @"en", @"es", @"fr", @"ja", @"ko", @"pt", @"ru", @"zh",
- @"zh-Hans",
+ @"zh-Hans", @"zh-Hant",
];
/**
@@ -92,7 +92,7 @@ static NSArray * const MGLMapboxStreetsLanguages = @[
*/
static NSArray * const MGLMapboxStreetsAlternativeLanguages = @[
@"mul", @"ar", @"de", @"es", @"fr", @"ja", @"ko", @"pt", @"ru", @"zh",
- @"zh-Hans",
+ @"zh-Hans", @"zh-Hant",
];
+ (NSSet<NSString *> *)mapboxStreetsLanguages {
@@ -105,10 +105,10 @@ static NSArray * const MGLMapboxStreetsAlternativeLanguages = @[
}
+ (NSString *)preferredMapboxStreetsLanguage {
- return [self preferredMapboxStreetsLanguageForPreferences:[NSLocale preferredLanguages]];
+ return [self preferredMapboxStreetsLanguagesForPreferences:[NSLocale preferredLanguages]].firstObject;
}
-+ (NSString *)preferredMapboxStreetsLanguageForPreferences:(NSArray<NSString *> *)preferencesArray {
++ (NSArray<NSString *> *)preferredMapboxStreetsLanguagesForPreferences:(NSArray<NSString *> *)preferencesArray {
BOOL acceptsEnglish = [preferencesArray filteredArrayUsingPredicate:
[NSPredicate predicateWithBlock:^BOOL(NSString * _Nullable language, NSDictionary<NSString *,id> * _Nullable bindings) {
NSString *languageCode;
@@ -123,16 +123,19 @@ static NSArray * const MGLMapboxStreetsAlternativeLanguages = @[
return [languageCode isEqualToString:@"en"];
}]].count;
- NSArray<NSString *> *availableLanguages = acceptsEnglish ? MGLMapboxStreetsLanguages : MGLMapboxStreetsAlternativeLanguages;
- NSArray<NSString *> *preferredLanguages = [NSBundle preferredLocalizationsFromArray:availableLanguages
- forPreferences:preferencesArray];
- NSString *mostSpecificLanguage;
- for (NSString *language in preferredLanguages) {
- if (language.length > mostSpecificLanguage.length) {
- mostSpecificLanguage = language;
- }
+ NSMutableArray<NSString *> *availableLanguages = (acceptsEnglish ? MGLMapboxStreetsLanguages : MGLMapboxStreetsAlternativeLanguages).mutableCopy;
+ NSMutableArray<NSString *> *preferredLanguages = [NSMutableArray array];
+ NSArray<NSString *> *matchingLanguages = @[];
+ while (![matchingLanguages isEqualToArray:@[availableLanguages.firstObject]]) {
+ [availableLanguages removeObjectsInArray:matchingLanguages];
+ matchingLanguages = [NSBundle preferredLocalizationsFromArray:availableLanguages
+ forPreferences:preferencesArray];
+ [preferredLanguages addObjectsFromArray:matchingLanguages];
+ }
+ if ([preferredLanguages.firstObject isEqualToString:@"mul"]) {
+ [preferredLanguages removeObjectAtIndex:0];
}
- return [mostSpecificLanguage isEqualToString:@"mul"] ? nil : mostSpecificLanguage;
+ return preferredLanguages;
}
- (BOOL)isMapboxStreets {
@@ -141,7 +144,7 @@ static NSArray * const MGLMapboxStreetsAlternativeLanguages = @[
return NO;
}
NSArray *identifiers = [url.host componentsSeparatedByString:@","];
- return [identifiers containsObject:@"mapbox.mapbox-streets-v7"] || [identifiers containsObject:@"mapbox.mapbox-streets-v6"];
+ return [identifiers containsObject:@"mapbox.mapbox-streets-v8"] || [identifiers containsObject:@"mapbox.mapbox-streets-v7"];
}
@end
diff --git a/platform/darwin/src/MGLVectorTileSource_Private.h b/platform/darwin/src/MGLVectorTileSource_Private.h
index 8d287ae4c4..2e1c614180 100644
--- a/platform/darwin/src/MGLVectorTileSource_Private.h
+++ b/platform/darwin/src/MGLVectorTileSource_Private.h
@@ -9,7 +9,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSSet<NSString *> *)mapboxStreetsLanguages;
+ (nullable NSString *)preferredMapboxStreetsLanguage;
-+ (nullable NSString *)preferredMapboxStreetsLanguageForPreferences:(NSArray<NSString *> *)preferencesArray;
++ (NSArray<NSString *> *)preferredMapboxStreetsLanguagesForPreferences:(NSArray<NSString *> *)preferencesArray;
@end
diff --git a/platform/darwin/src/NSExpression+MGLAdditions.mm b/platform/darwin/src/NSExpression+MGLAdditions.mm
index 04152afffd..471b131003 100644
--- a/platform/darwin/src/NSExpression+MGLAdditions.mm
+++ b/platform/darwin/src/NSExpression+MGLAdditions.mm
@@ -1409,11 +1409,12 @@ NSDictionary<NSNumber *, NSExpression *> *MGLLocalizedStopDictionary(NSDictionar
}
case NSKeyPathExpressionType: {
+ // TODO: Make this safe to call multiple times by putting the whole expression inside a variable context.
if ([self.keyPath isEqualToString:@"name"] || [self.keyPath hasPrefix:@"name_"]) {
NSString *localizedKeyPath = @"name";
if (![locale.localeIdentifier isEqualToString:@"mul"]) {
NSArray *preferences = locale ? @[locale.localeIdentifier] : [NSLocale preferredLanguages];
- NSString *preferredLanguage = [MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences];
+ NSString *preferredLanguage = [MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences].firstObject;
if (preferredLanguage) {
localizedKeyPath = [NSString stringWithFormat:@"name_%@", preferredLanguage];
}
diff --git a/platform/darwin/test/MGLStyleTests.mm b/platform/darwin/test/MGLStyleTests.mm
index 7330477126..97ce381c20 100644
--- a/platform/darwin/test/MGLStyleTests.mm
+++ b/platform/darwin/test/MGLStyleTests.mm
@@ -430,27 +430,27 @@
- (void)testLanguageMatching {
{
NSArray *preferences = @[@"en"];
- XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences], @"en");
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[@"en"]);
}
{
NSArray *preferences = @[@"en-US"];
- XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences], @"en");
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[@"en"]);
}
{
NSArray *preferences = @[@"fr"];
- XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences], @"fr");
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[@"fr"]);
}
{
NSArray *preferences = @[@"zh-Hans"];
- XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences], @"zh-Hans");
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[@"zh-Hans"]);
}
{
NSArray *preferences = @[@"zh-Hans", @"en"];
- XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences], @"zh-Hans");
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[@"zh-Hans"]);
}
{
NSArray *preferences = @[@"zh-Hant"];
- XCTAssertNil([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences]);
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[]);
}
{
NSArray *preferences = @[@"en", @"fr", @"el"];
@@ -458,15 +458,15 @@
}
{
NSArray *preferences = @[@"tlh"];
- XCTAssertNil([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences]);
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[]);
}
{
NSArray *preferences = @[@"tlh", @"en"];
- XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences], @"en");
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[@"en"]);
}
{
NSArray *preferences = @[@"mul"];
- XCTAssertNil([MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences]);
+ XCTAssertEqualObjects([MGLVectorTileSource preferredMapboxStreetsLanguagesForPreferences:preferences], @[]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.