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

Commit

Permalink
Add fallbacks for name fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydsheng committed Jul 26, 2018
1 parent 8400355 commit d3223e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
18 changes: 17 additions & 1 deletion platform/darwin/src/NSExpression+MGLAdditions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,23 @@ - (NSExpression *)mgl_expressionLocalizedIntoLocale:(nullable NSLocale *)locale
localizedKeyPath = [NSString stringWithFormat:@"name_%@", preferredLanguage];
}
}
return [NSExpression expressionForKeyPath:localizedKeyPath];
// If the keypath is `name` or `name_en`, no need to fallback
if ([localizedKeyPath isEqualToString:@"name"] || [localizedKeyPath isEqualToString:@"name_en"]) {
return [NSExpression expressionForKeyPath:localizedKeyPath];
}
// If the keypath is `name_zh-Hans`, fallback to `name_zh`. If `name_zh` is empty, fallback to `name`
// The `name_zh-Hans` field was added since Mapbox Streets v7
// See the documentation of name fields for detail https://www.mapbox.com/vector-tiles/mapbox-streets-v7/#overview
if ([localizedKeyPath isEqualToString:@"name_zh-Hans"]) {
return [NSExpression expressionWithFormat:@"mgl_coalesce(%@)",
@[[NSExpression expressionForKeyPath:localizedKeyPath],
[NSExpression expressionForKeyPath:@"name_zh"],
[NSExpression expressionForKeyPath:@"name"],]];
}
// Other keypath fallback to `name`
return [NSExpression expressionWithFormat:@"mgl_coalesce(%@)",
@[[NSExpression expressionForKeyPath:localizedKeyPath],
[NSExpression expressionForKeyPath:@"name"],]];
}
return self;
}
Expand Down
13 changes: 10 additions & 3 deletions platform/darwin/test/MGLExpressionTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1023,12 +1023,17 @@ - (void)testLocalization {
}
{
NSExpression *original = [NSExpression expressionForKeyPath:@"name_en"];
NSExpression *expected = [NSExpression expressionForKeyPath:@"name_fr"];
NSExpression *expected = [NSExpression expressionWithFormat:@"mgl_coalesce(%@)",
@[[NSExpression expressionForKeyPath:@"name_fr"],
[NSExpression expressionForKeyPath:@"name"]]];
XCTAssertEqualObjects([original mgl_expressionLocalizedIntoLocale:[NSLocale localeWithLocaleIdentifier:@"fr-CA"]], expected);
}
{
NSExpression *original = [NSExpression expressionForKeyPath:@"name_en"];
NSExpression *expected = [NSExpression expressionForKeyPath:@"name_zh-Hans"];
NSExpression *expected = [NSExpression expressionWithFormat:@"mgl_coalesce(%@)",
@[[NSExpression expressionForKeyPath:@"name_zh-Hans"],
[NSExpression expressionForKeyPath:@"name_zh"],
[NSExpression expressionForKeyPath:@"name"]]];
XCTAssertEqualObjects([original mgl_expressionLocalizedIntoLocale:[NSLocale localeWithLocaleIdentifier:@"zh-Hans"]], expected);
}
{
Expand All @@ -1045,7 +1050,9 @@ - (void)testLocalization {
NSExpression *expected = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, short, %@)", @{
@1: [NSExpression expressionForKeyPath:@"abbr"],
@2: @"",
@3: [NSExpression expressionForKeyPath:@"name_es"],
@3: [NSExpression expressionWithFormat:@"mgl_coalesce(%@)",
@[[NSExpression expressionForKeyPath:@"name_es"],
[NSExpression expressionForKeyPath:@"name"]]]
}];
XCTAssertEqualObjects([original mgl_expressionLocalizedIntoLocale:[NSLocale localeWithLocaleIdentifier:@"es-PR"]], expected);
}
Expand Down
1 change: 1 addition & 0 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
* Added the `collator` and `resolved-locale` expression operators to more precisely compare strings in style JSON. A subset of this functionality is available through predicate options when creating an `NSPredicate`. ([#11869](https://github.com/mapbox/mapbox-gl-native/pull/11869))
* Fixed a crash when trying to parse expressions containing legacy filters. ([#12263](https://github.com/mapbox/mapbox-gl-native/pull/12263))
* Fixed a crash that occurred when creating an `MGL_MATCH` expression using non-expressions as arguments. ([#12332](https://github.com/mapbox/mapbox-gl-native/pull/12332))
* Fixed an issue that no localized low-zoom labels when system language is Simplified Chinese ([#12164](https://github.com/mapbox/mapbox-gl-native/issues/12164))

### Networking and storage

Expand Down

0 comments on commit d3223e1

Please sign in to comment.