【问题标题】:how to localize language of the city with location in swift 4 for ios 9.0?如何在 swift 4 for ios 9.0 中本地化城市语言?
【发布时间】:2018-04-28 10:19:07
【问题描述】:

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


    【解决方案1】:

    在 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)都会受到影响。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      相关资源
      最近更新 更多