Reputation: 1482
Swift 2 contains function:
NSBundle.setLanguage("...")
But new class "Bundle" doesn't contain method setLanguage in Swift 3. What is the best way to set language in Swift 3?
Upvotes: 0
Views: 2123
Reputation: 1504
setLanguage()
seems to have been deprecated in Swift 3 Bundle
or you're using an NSBundle
extension. Instead, here's what you can do:
let path = Bundle.main.path(forResource: lang, ofType: "lproj")
let bundle = Bundle(path: path!)
Then you can use that bundle
to get a localized string. Here's an extension I wrote for just that:
extension String {
func localized(lang:String) -> String? {
if let path = Bundle.main.path(forResource: lang, ofType: "lproj") {
if let bundle = Bundle(path: path) {
return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
}
}
return nil;
}
}
Usage
"any string from the strings file".localized("en") // or "sv" for Swedish or "fi" for Finnish
Upvotes: 5