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

Automatic expression localization should try to use requested locale even if not officially supported by Streets source #445

Open
1ec5 opened this issue Sep 15, 2020 · 1 comment

Comments

@1ec5
Copy link
Contributor

1ec5 commented Sep 15, 2020

-[NSExpression mgl_expressionLocalizedIntoLocale:] only considers the passed-in locale if it’s one of the locales with a dedicated name_* property in the Mapbox Streets source. This check can get outdated: if a minor update to the Streets source adds a new language, users of that language don’t automatically see that language until we update a list on the client-side, as in #173. What’s more, if the application is pulling in a tileset other than the Streets source that follows the name_* convention, it would be nice if this method would work without forcing the application to muck with expressions directly.

The fix is to fashion another name_* key path expression based on the locale argument – without limiting it to the Streets languages – and inserting it at the front of this coalescing expression:

NSArray *preferences = locale ? @[locale.localeIdentifier] : [NSLocale preferredLanguages];
NSString *preferredLanguage = [MGLVectorTileSource preferredMapboxStreetsLanguageForPreferences:preferences];
if (preferredLanguage) {
localizedKeyPath = [NSString stringWithFormat:@"name_%@", preferredLanguage];
}
return [NSExpression expressionWithFormat:@"mgl_coalesce({%K, %K})", localizedKeyPath, @"name"];

/cc @mapbox/maps-ios

@1ec5
Copy link
Contributor Author

1ec5 commented Sep 15, 2020

As a workaround, if you know the language you want to display and know that your custom tileset supports it, you can set the MGLSymbolStyleLayer.text property directly without using this method:

// Objective-C

// Latin
labels.text = [NSExpression expressionWithFormat:@"name_la"];
// Latin, falling back to Ancient Greek or the native local name
labels.text = [NSExpression expressionWithFormat:@"mgl_coalesce({name_la, name_grc, name})"];
// Swift

// Latin
labels.text = NSExpression(withFormat: "name_la")
// Latin, falling back to Ancient Greek or the native local name
labels.text = NSExpression(withFormat: "mgl_coalesce({name_la, name_grc, name})")

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants