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

Localization issue: German translations of city is wrong #9171

Closed
evgzor opened this issue Jun 2, 2017 · 4 comments
Closed

Localization issue: German translations of city is wrong #9171

evgzor opened this issue Jun 2, 2017 · 4 comments
Labels
localization Human language support and internationalization

Comments

@evgzor
Copy link

evgzor commented Jun 2, 2017

iOS and Android:
3.5.4 / 5.0

Steps to trigger behavior

  1. Switch to the German localization
  2. Navigate to Germany - Cities (Munich, Cologne)
  3. Observe the result

Expected behavior

Mapbox is displaying the names of Cities (München, Köln) in German.

Actual behavior

Mapbox is displaying the names of some Cities (Munich, Cologne) in English.

screenshot 2017-06-02 10 40 34

@fabian-guerra fabian-guerra added the localization Human language support and internationalization label Jun 2, 2017
@jfirebaugh
Copy link
Contributor

Map labels are not automatically localized. This feature is being tracked in #7031.

@1ec5
Copy link
Contributor

1ec5 commented Jun 9, 2017

In the meantime, see this example code that automatically localizes into the user’s locale:

- (void)updateLabels {
MGLStyle *style = self.mapView.style;
NSString *preferredLanguage = _isLocalizingLabels ? [MGLVectorSource preferredMapboxStreetsLanguage] : nil;
NSMutableDictionary *localizedKeysByKeyBySourceIdentifier = [NSMutableDictionary dictionary];
for (MGLSymbolStyleLayer *layer in style.layers) {
if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) {
continue;
}
MGLVectorSource *source = (MGLVectorSource *)[style sourceWithIdentifier:layer.sourceIdentifier];
if (![source isKindOfClass:[MGLVectorSource class]] || !source.mapboxStreets) {
continue;
}
NSDictionary *localizedKeysByKey = localizedKeysByKeyBySourceIdentifier[layer.sourceIdentifier];
if (!localizedKeysByKey) {
localizedKeysByKey = localizedKeysByKeyBySourceIdentifier[layer.sourceIdentifier] = [source localizedKeysByKeyForPreferredLanguage:preferredLanguage];
}
NSString *(^stringByLocalizingString)(NSString *) = ^ NSString * (NSString *string) {
NSMutableString *localizedString = string.mutableCopy;
[localizedKeysByKey enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSString * _Nonnull localizedKey, BOOL * _Nonnull stop) {
NSAssert([key isKindOfClass:[NSString class]], @"key is not a string");
NSAssert([localizedKey isKindOfClass:[NSString class]], @"localizedKey is not a string");
[localizedString replaceOccurrencesOfString:[NSString stringWithFormat:@"{%@}", key]
withString:[NSString stringWithFormat:@"{%@}", localizedKey]
options:0
range:NSMakeRange(0, localizedString.length)];
}];
return localizedString;
};
if ([layer.text isKindOfClass:[MGLConstantStyleValue class]]) {
NSString *textField = [(MGLConstantStyleValue<NSString *> *)layer.text rawValue];
layer.text = [MGLStyleValue<NSString *> valueWithRawValue:stringByLocalizingString(textField)];
}
else if ([layer.text isKindOfClass:[MGLCameraStyleFunction class]]) {
MGLCameraStyleFunction *function = (MGLCameraStyleFunction<NSString *> *)layer.text;
NSMutableDictionary *stops = function.stops.mutableCopy;
[stops enumerateKeysAndObjectsUsingBlock:^(NSNumber *zoomLevel, MGLConstantStyleValue<NSString *> *stop, BOOL *done) {
NSString *textField = stop.rawValue;
stops[zoomLevel] = [MGLStyleValue<NSString *> valueWithRawValue:stringByLocalizingString(textField)];
}];
function.stops = stops;
layer.text = function;
}
}
}

https://github.com/mapbox/mapbox-gl-native/blob/66c2a2a0b7c42e1947f57a412ece66fa8eaac149/platform/macos/app/MGLVectorSource+MBXAdditions.m

mapbox/mapbox-plugins-ios#2 tracks building a plugin that encapsulates this code, but for now, you can manually incorporate it into your application.

@evgzor
Copy link
Author

evgzor commented Jun 9, 2017

@1ec5 I have no access to mapbox/mapbox-plugins-ios#2
could u provide it?

@1ec5
Copy link
Contributor

1ec5 commented Jun 9, 2017

Not much to see there yet. 😉 But we’re working on it and will open it up as soon as there’s something usable. In the meantime, please try copying the source code linked above.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
localization Human language support and internationalization
Projects
None yet
Development

No branches or pull requests

4 participants