viveksinghmehta
2/18/2019 - 5:23 AM

This convert English language into any other language.

extension String {
  var localized: String {
        if let language = UserDefaults.standard.object(forKey: "AppleLanguages") as? String {
            switch language {
            case "en" :
                guard let path = Bundle.main.path(forResource: "en", ofType: "lproj") else { return NSLocalizedString(self, comment: self) }
                guard let bundle = Bundle(path: path) else { return NSLocalizedString(self, comment: self) }
                return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: self)
            case "fr" :
                guard let path = Bundle.main.path(forResource: "fr", ofType: "lproj") else { return NSLocalizedString(self, comment: self) }
                guard let bundle = Bundle(path: path) else { return NSLocalizedString(self, comment: self) }
                return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: self)
            default :
                return NSLocalizedString(self, comment: self)
            }
        } else {
            return NSLocalizedString(self, comment: self)
        }
    }
}