2

I've encountered a rather strange situation where the backend developer asked me to force the city name that is returned from performing a geolocation to be in a specific language? For instance have it return København instead of Copenhagen no matter what language the iOS is to. I couldn't find anything on the web regarding this. Your help will be much appreciated. Thanks

alex
  • 957
  • 2
  • 9
  • 16
  • I'm a little confused by the question. Are you trying to reverse geocode a location (i.e. given a location, get the city name that location in a particular language)? – Jeff Ames Aug 21 '13 at 02:16
  • something like that. I'm trying to geocode an address and have core location framework return me the city in a particular language. But from what I saw the framework is using locale language to return the city in the language that the user has set on his phone. I just need it to be in e.g. danish no matter what language the user has set on his ios – alex Aug 21 '13 at 06:20

1 Answers1

2

Not sure if this is the best and safest solution but you could temporarily change the locale while you make the reverse geo code call and then reset it when you are done.

NSArray *defaultLanguages = [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"];
NSArray *languages = [NSArray arrayWithObject:@"da"];
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *location = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = placemarks[0];
        NSString *danish = placemark.country;
        [[NSUserDefaults standardUserDefaults] setObject:defaultLanguages forKey:@"AppleLanguages"];
        [[NSUserDefaults standardUserDefaults] synchronize];
}];
Jeff Ames
  • 1,555
  • 11
  • 27
  • 1
    Let me add here that the proposed solution has a very bad bug: it effectively stores languages in the application domain, therefore overriding any further change of AppleLanguages in the system (e.g. when user changes the language of iOS device to some other value - as result application UI won't take those language changes into account). Let me suggest to use `[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AppleLanguages"]` to revert the override. – Grigory Entin Feb 19 '16 at 16:47
  • @GrigoryEntin Yes, I had mentioned that you might want to reset the locale when done with the geocode but can't hurt to emphasize the harms of not doing so. Thanks – Jeff Ames Feb 23 '16 at 09:36
  • Well, I'm not sure I understand you correctly. The thing is that `[[NSUserDefaults standardUserDefaults] setObject:defaultLanguages forKey:@"AppleLanguages"]` in your solution (in the completion handler) does not _correctly_ reset the locale when done. It effectively stores the current value of defaultLanguages in the application domain instead of removing the custom one. As result if the system wide value for AppleLanguages changes, it won't be reflected in the application defaults as it will be overriden by the stored value (that no longer matches the current/system-wide value). – Grigory Entin Feb 24 '16 at 10:19
  • @GrigoryEntin ah, so you're saying that by default [[NSUserDefaults standardUserDefaults] setObject:defaultLanguages forKey:@"AppleLanguages"] returns the system locale but if you overwrite it, it will return the whatever value you set it to, regardless if the user changed it in settings. And if you remove AppleLanguages from standard user defaults then [[NSUserDefaults standardUserDefaults] valueForKey:@"AppleLanguages"] will always return the system locale? I haven't tested it but if that's true, you're right the correct way to reset the locale is how you suggested – Jeff Ames Mar 02 '16 at 14:53
  • Yes, exactly. That's what I faced and that's how NSUserDefaults domains work. According to Apple docs `-setObject:forKey: ` "Sets the value of the specified default key in the standard application domain". But (system-wide) "AppleLanguages" is set in NSGlobalDomain. Application domain preceeds NSGlobalDomain in user defaults search list, so setting a value with `-setObject:forKey:` will override any further change of the value in NSGlobalDomain. – Grigory Entin Mar 03 '16 at 07:47
  • @JeffAmes You are Awesome man, Thanks you save my time thanks. – M.Shuaib Imran May 18 '16 at 08:26