如何为 ios 9.0 本地化位于 swift 4 的城市语言?
how to localize language of the city with location in swift 4 for ios 9.0?
我在我的应用程序中使用了位置,它将在应用程序中获取纬度和经度,然后可以给我用户所在城市的名称
这个名字是英文的,但我想把它转换成波斯语
我找到了这个方法但这将在 iOS 11 中工作但是我正在从 ios9 开发我怎样才能用波斯语打印当前城市?
这是我的代码
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if firstUpdate {
if let _latitude = locations.first?.coordinate.latitude {
if let _longitude = locations.first?.coordinate.longitude {
firstUpdate = false
self.locationManager.stopUpdatingLocation()
self.carregaDadosLocal(latitude: _latitude, longitude: _longitude)
let location = CLLocation(latitude: _latitude, longitude: _longitude)
if #available(iOS 11.0, *) {
CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale(identifier: "fa_IR")) { (local, error) in
if (local?[0]) != nil {
print(local?[0].thoroughfare!)
print(local?[0].locality)
print(local?[0].location)
print(local?[0].subLocality)
}
}
} else {
// Fallback on earlier versions
}
}
}
}
}
在 iOS 9,您可以暂时覆盖应用的语言:
func completionHandler(_ placemarks: [CLPlacemark]?, _ error: Error?) {
if let place = placemarks?.first {
print(place.thoroughfare!)
print(place.locality)
print(place.location)
print(place.subLocality)
}
}
let location = CLLocation(latitude: 48.858370, longitude: 2.294481)
if #available(iOS 11.0, *) {
CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale(identifier: "fa_IR"), completionHandler: completionHandler)
} else {
UserDefaults.standard.set(["fa_IR"], forKey: "AppleLanguages")
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
// Remove the language override
UserDefaults.standard.removeObject(forKey: "AppleLanguages")
completionHandler(placemarks, error)
}
}
要注意的是,无论用户设置如何,您的应用都会在短时间内变成波斯语。任何依赖于语言环境的内容(如 NSLocalizedString
或 NumberFormatter
)都会受到影响。
我在我的应用程序中使用了位置,它将在应用程序中获取纬度和经度,然后可以给我用户所在城市的名称 这个名字是英文的,但我想把它转换成波斯语 我找到了这个方法但这将在 iOS 11 中工作但是我正在从 ios9 开发我怎样才能用波斯语打印当前城市? 这是我的代码
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if firstUpdate {
if let _latitude = locations.first?.coordinate.latitude {
if let _longitude = locations.first?.coordinate.longitude {
firstUpdate = false
self.locationManager.stopUpdatingLocation()
self.carregaDadosLocal(latitude: _latitude, longitude: _longitude)
let location = CLLocation(latitude: _latitude, longitude: _longitude)
if #available(iOS 11.0, *) {
CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale(identifier: "fa_IR")) { (local, error) in
if (local?[0]) != nil {
print(local?[0].thoroughfare!)
print(local?[0].locality)
print(local?[0].location)
print(local?[0].subLocality)
}
}
} else {
// Fallback on earlier versions
}
}
}
}
}
在 iOS 9,您可以暂时覆盖应用的语言:
func completionHandler(_ placemarks: [CLPlacemark]?, _ error: Error?) {
if let place = placemarks?.first {
print(place.thoroughfare!)
print(place.locality)
print(place.location)
print(place.subLocality)
}
}
let location = CLLocation(latitude: 48.858370, longitude: 2.294481)
if #available(iOS 11.0, *) {
CLGeocoder().reverseGeocodeLocation(location, preferredLocale: Locale(identifier: "fa_IR"), completionHandler: completionHandler)
} else {
UserDefaults.standard.set(["fa_IR"], forKey: "AppleLanguages")
CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
// Remove the language override
UserDefaults.standard.removeObject(forKey: "AppleLanguages")
completionHandler(placemarks, error)
}
}
要注意的是,无论用户设置如何,您的应用都会在短时间内变成波斯语。任何依赖于语言环境的内容(如 NSLocalizedString
或 NumberFormatter
)都会受到影响。