7

Using the code below I'm requesting a reverse geocoding for the current coordinate.
Everything works great, except for when the device is set to a different language than English.
Example: If the language of the device is Italian, and I'm in Italy, the result for country is "Italia" instead of "Italy".

How can I force the results to be only in English, regardless of the language of the device?

CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    for (CLPlacemark * placemark in placemarks) {

        [locationController setLastKnownCountry:[placemark country]];
    }
}];

Thank you.

thedp
  • 8,350
  • 16
  • 53
  • 95

3 Answers3

19

You have to use the NSLocale class that manages info in every language. Then for your final translation you have to force the locale to be in english language:

CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

    for (CLPlacemark * placemark in placemarks) {
         //Obtains the country code, that is the same whatever language you are working with (US, ES, IT ...)
         NSString *countryCode = [placemark ISOcountryCode];
         //Obtains a locale identifier. This will handle every language
         NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
         //Obtains the country name from the locale BUT IN ENGLISH (you can set it as "en_UK" also)
         NSString *country = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] displayNameForKey: NSLocaleIdentifier value:identifier];

        //Continues your code
        [locationController setLastKnownCountry:country];
    }
}];
lihudi
  • 860
  • 1
  • 12
  • 21
  • This works well, but is there any way to get the rest of the stuff from `CLPlacemark` in "hardcoded" english locale? (i.e., State, City etc.) – Aviel Gross Mar 31 '15 at 06:22
  • 1
    @AvielGross: I don't think you can do that with other data than the country. If you need to do it you should change the NSStandardDefaults as set here: http://stackoverflow.com/questions/13359152/clplacemark-locality-value-changes-if-the-device-language-is-different – lihudi Apr 06 '15 at 14:55
  • @lihudi this was my initial try to solve this, I moved to yours since the defaults "trick" didn't work for me... Might be something with iOS8/.2... – Aviel Gross Apr 06 '15 at 14:58
  • what is locationController – Pintu Rajput Jun 21 '16 at 14:00
  • I don’t know. It was on the asker's code. I left it there to show him/her where to continue writing his/her code – lihudi Jun 22 '16 at 17:18
6

It works with the iOS 11 API: reverseGeocodeLocation(_:preferredLocale:completionHandler:)

Sadly I need to support other iOS versions and changing the UserDefaults does neither feel good nor work for me :-(

lucasl
  • 498
  • 6
  • 15
-4

You can try the below code to extract the name of the country from the placemark which will show the result in english only regardless the county name

- (void)findAddress:(CLLocationDegrees)latitude with:(CLLocationDegrees)longitude
{
    CLLocation *location =[[CLLocation alloc]initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Finding address");
        if (error) {
            NSLog(@"Error %@", error.description);
        } else {
            NSLog(@"%@",[placemarks valueForKey:@"country"]);
        }
    }];
}
Raj
  • 413
  • 1
  • 4
  • 5
  • 2
    It's pretty much the code I've posted in the question. And no, it does not give the result in English since the request to Apple's servers already contains the "desired" language. – thedp Aug 21 '14 at 11:56