ios - 如何在 ios 9.0 的 swift 4 中使用位置本地化城市语言?

标签 ios swift4 cllocationmanager cllocation nslocalizedstring

我在我的应用程序中使用了位置,它将在应用程序中获取纬度和经度,然后可以给我用户所在城市的名称 这个名字是英文的,但我想把它转换成波斯语 我找到了这个方法但这将在 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)
    }
}

要注意的是,无论用户设置如何,您的应用都会在短时间内变成波斯语。任何依赖于语言环境的内容(如 NSLocalizedStringNumberFormatter)都会受到影响。

关于ios - 如何在 ios 9.0 的 swift 4 中使用位置本地化城市语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075423/

相关文章:

ios - Swift 结构改变一个变量不起作用?

ios - 奇怪的 iPad2 位置问题

ios - 应用程序处于非事件状态时获取位置更新在 2 次更新后停止

ios - 从 firebase 检索用户信息

ios - UISwitch改变对象解析 objective-c

ios - iOS 中的 Paypal 集成第一次不显示登录窗口?

ios - 如何在 ios 中处理大文件上传?

swift - ChannelInboundHandler (Swift-NIO) 中的 writeDataUnsupported

ios - 当前位置在谷歌地图中不起作用

android - 错误垂直视口(viewport)被赋予无限高度