How to get system device language, swift iOS
Try this
UserDefaults.standard.stringArray(forKey: "AppleLanguages")
The output will be like - ["en-US"]
.
It'll return an array with language codes, first index is the current language set in iPhone's setting (it'll be a single item array if the preferred language order in settings is empty.)
How to get detailed language of device in swift
Start with the currentLocale()
and ask questions about it. For example:
let lang = NSLocale.currentLocale().localeIdentifier
Or, at a finer level of granularity:
let langId = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode) as! String
let countryId = NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
let language = "\(langId)-\(countryId)" // en-US on my machine
How to get human friendly current local language name in iOS?
You can use localizedString(forLanguageCode:) method on Locale object.
let locale: Locale = .current
locale.localizedString(forLanguageCode: "pl_PL") // "Polish"
along with other useful functions that can help you get localized region or currency names from codes.
locale.localizedString(forRegionCode: "pl") // "Poland"
locale.localizedString(forCurrencyCode: "PLN") // "Polish Zloty"
and to use different locale than the .current
you can easily initialize it with one of the available identifiers.
let japanese = Locale(identifier: "ja_JP")
japanese.localizedString(forLanguageCode: "pl_PL") // "ポーランド語"
japanese.localizedString(forRegionCode: "pl") // "ポーランド"
japanese.localizedString(forCurrencyCode: "PLN") // "ポーランド ズウォティ"
Getting current device language in iOS?
The solutions provided will actually return the current region of the device - not the currently selected language. These are often one and the same. However, if I am in North America and I set my language to Japanese, my region will still be English (United States). In order to retrieve the currently selected language, you can do:
NSString * language = [[NSLocale preferredLanguages] firstObject];
This will return a two letter code for the currently selected language. "en" for English, "es" for Spanish, "de" for German, etc. For more examples, please see this Wikipedia entry (in particular, the 639-1 column):
List of ISO 639-1 codes
Then it's a simple matter of converting the two letter codes to the string you would like to display. So if it's "en", display "English".
EDIT
Worth to quote the header information from NSLocale.h:
+ (NSArray *)preferredLanguages NS_AVAILABLE(10_5, 2_0); // note that this list does not indicate what language the app is actually running in; the [NSBundle mainBundle] object determines that at launch and knows that information
People interested in app language take a look at @mindvision's answer
Check language in iOS app
Swift 3
You can take the language code like this
let preferredLanguage = NSLocale.preferredLanguages[0]
And then you need to compare it with code string
if preferredLanguage == "en" {
print("this is English")
} else if preferredLanguage == "uk" {
print("this is Ukrainian")
}
You can find codes here
An example to check if French ...
/// Is Device use french language
/// Consider, "fr-CA", "fr-FR", "fr-CH" et cetera
///
/// - Returns: Bool
static func isFrench() -> Bool {
return NSLocale.preferredLanguages[0].range(of:"fr") != nil
}
How to get the language code back from a localizedString?
We can bind Picker
selection directly to AppStorage
and use code as tag to match, so code is simplified to
struct ContentView: View {
@AppStorage("language") var language: String = "en"
var body: some View {
VStack {
Text("Selected: " + language)
Picker("", selection: $language) {
ForEach(Bundle.main.localizations, id: \.self) {
Text((Locale.current as NSLocale)
.localizedString(forLanguageCode: $0)!).tag($0) // << here !!
}
}
}
}
}
and selection separated from presentation.
Tested with Xcode 13.4 / iOS 15.5
Test module on GitHub
Related Topics
How to Convert Double to Int in Swift
Nsdate() or Date() Shows the Wrong Time
Swift - How to Convert String to Double
Is Key-Value Observation (Kvo) Available in Swift
How to Get Ip Address in Swift
How to Make a Weak Protocol Reference in 'Pure' Swift (Without @Objc)
How to Stop a Dispatchworkitem in Gcd
How to Detect If an Skspritenode Has Been Touched
Binary Operator * Cannot Be Applied to Operands of Type Int and Double
Get Integer Value from String in Swift
Can Swift Convert a Class/Struct Data into Dictionary
Can Swift Return Value from an Async Void-Returning Block
Nsattributedstring, Change the Font Overall But Keep All Other Attributes
Swift Variable Name With ' (Backtick)
Tabview Resets Navigation Stack When Switching Tabs
Extensions in My Own Custom Class
Is There a Swift Alternative For Nslog(@"%S", _Pretty_Function_)