我正在使用本地化和 clocationManager
当更改语言时一切正常,只有国家和城市名称与以前的语言一起使用,本地化完全没有影响,唯一的方法是关闭应用程序并再次打开它以使用新语言! 在不关闭应用程序的情况下更改国家和城市的任何解决方案?
本地化代码
static func setLanguage(lang:String) -> Void {
UserDefaults.standard.removeObject(forKey: "AppleLanguages")
UserDefaults.standard.set([lang], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
Bundle.setLanguage(lang)
}
CLLocationManager代码
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locValue = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
if placeMark != nil{
// City
if let city = placeMark.addressDictionary!["State"] as? NSString {
self.locCity = city as String
}
// Country
if let country = placeMark.addressDictionary!["Country"] as? NSString {
self.locCountry = "\(country)," as String
}
self.countryLocation.text = "\(self.locCountry) \(self.locCity)"
self.locationManager.stopUpdatingLocation()
}
})
}
最佳答案
您必须在 NSLocale.currentLocaleDidChangeNotification
上添加观察者以检测任何此类更改。
像这样:
NotificationCenter.default.addObserver(self, selector: #selector(MyClass.languageDidChange),
name: NSLocale.currentLocaleDidChangeNotification, object: nil)
languageDidChange
方法将在 NSLocale
发生变化时被调用。
希望这对您有所帮助。
关于ios - 语言改变了但 CLLocationManager 没有受到影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395467/