I want get the language code of the device (en, es...) in my app written with Swift. How can get this?
I'm trying this:
var preferredLanguages : NSLocale!
let pre = preferredLanguages.displayNameForKey(NSLocaleIdentifier, value: preferredLanguages)
But this returns nil.
Thanks!
In Swift 3
let langStr = Locale.current.languageCode
It's important to make the difference between the App language and the device locale language (The code bellow is in Swift 3)
Will return the Device language:
let locale = NSLocale.current.languageCode
Will return the App language:
let pre = Locale.preferredLanguages[0]
To get current language used in your app (different than preferred languages)
NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode)!
In Swift 3:
NSLocale.current.languageCode
In Swift 5.1:
Locale.current.languageCode
Swift 3 & 4 & 4.2 & 5
Locale.current.languageCode
does not compile regularly. Because you did not implemented localization for your project.
You have two possible solutions
1) String(Locale.preferredLanguages[0].prefix(2))
It returns phone lang properly.
If you want to get the type en-En
, you can use Locale.preferredLanguages[0]
2)
Select Project(MyApp)
->Project (not Target)
-> press + button into Localizations
, then add language which you want.
swift 3
let preferredLanguage = Locale.preferredLanguages[0] as String
print (preferredLanguage) //en-US
let arr = preferredLanguage.components(separatedBy: "-")
let deviceLanguage = arr.first
print (deviceLanguage) //en
you may use the below code it works fine with swift 3
var preferredLanguage : String = Bundle.main.preferredLocalizations.first!
I want to track the language chosen by the user in Settings app every time the user launches my app - that is not yet localized (my app is in English only). I adopted this logic:
create an enum to to make it easier to handle the languages in array
enum Language: String { case none = "" case en = "English" case fr = "French" case it = "Italian" } // add as many languages you want
create a couple of extension to Locale
extension Locale { static var enLocale: Locale { return Locale(identifier: "en-EN") } // to use in **currentLanguage** to get the localizedString in English static var currentLanguage: Language? { guard let code = preferredLanguages.first?.components(separatedBy: "-").last else { print("could not detect language code") return nil } guard let rawValue = enLocale.localizedString(forLanguageCode: code) else { print("could not localize language code") return nil } guard let language = Language(rawValue: rawValue) else { print("could not init language from raw value") return nil } print("language: \(code)-\(rawValue)") return language } }
When you need, you can simply use the extension
if let currentLanguage = Locale.currentLanguage { print(currentLanguage.rawValue) // Your code here. }
In Swift, You can get the locale using.
let locale = Locale.current.identifier
来源:https://stackoverflow.com/questions/24591167/how-to-get-current-language-code-with-swift