the*_*edp 7 xcode localization geolocation reverse-geocoding ios7
使用下面的代码,我正在为当前坐标请求反向地理编码.
一切都很好,除了设备设置为与英语不同的语言.
示例:如果设备的语言是意大利语,而我在意大利,则国家的结果是"Italia"而不是"Italy".
无论设备的语言如何,我如何强制结果只能用英语?
CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:myCurrentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
[locationController setLastKnownCountry:[placemark country]];
}
}];
Run Code Online (Sandbox Code Playgroud)
谢谢.
lih*_*udi 18
您必须使用管理每种语言的信息的NSLocale类.然后,对于您的最终翻译,您必须强制使用英语语言环境:
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];
}
}];
Run Code Online (Sandbox Code Playgroud)
它适用于 iOS 11 API: reverseGeocodeLocation(_:preferredLocale:completionHandler:)
可悲的是,我需要支持其他 iOS 版本,而更改 UserDefaults 对我来说既不舒服也不适合我:-(