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
}
iOS:How to get current application language
What i always do:
Add a string entry into the Localizable.strings files.
I always use the key "lang"="de";
(or "lang"="en"
, etc.).
Then you can use it in your NSURLRequest by adding the language over NSLocalizedString(@"lang", @"")
With that method you have absolute control what is going to be sent to you backend.
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 programmatically access the apps language in iOS 13?
You may try the below code to get the application preferred language.
let appLang = Locale.preferredLanguages[0]
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
How to get the localised languages of my iOS app?
Use:
let languageIds = Bundle.main.localizations
to get the list of language ids. Then you can convert each language id into the localized language name with:
let loc = Locale(identifier: langId)
let name = loc.localizedString(forLanguageCode: langId) ?? "Unknown"
Here's some code that builds a dictionary of available languages in your app's bundle. It is keyed on the locale id (language id) and each value is the localized language name for each locale id.
let langIds = Bundle.main.localizations
var languages = [String:String]()
for langId in langIds {
let loc = Locale(identifier: langId)
if let name = loc.localizedString(forLanguageCode: langId) {
languages[langId] = name
} else {
// this should never be reached
}
}
The Bundle
class also provides the preferredLocalizations
property and a few other related properties and functions.
Why does my iOS app only detect the current language of device correctly on first run?
Go to "Edit Scheme..." and make sure that "Application Language" is set to "System Language":
Related Topics
How to Change Status Bar Text Color Immediately in Swift in iOS 13
How to Instantiate and Load a View Controller Before Segueing to It Using Swift
Swift - Update/Refresh Label That Displays Time
Cgaffinetransform -How to Align Video in Screen Center
Firebase: Provided Bucket Does Not Match the Storage Bucket of the Current Instance in Swift
Dynamically Passing Closure with Keypaths to a Sorting Function
Attempting to Segue from Objective-C to Swift Vc
Xcode 11 Archive Gives Phasescriptexecution Failed
Change Selected Row Label Color in Picker View Swift 1.2
Uitableviewcell Shows Incorrect Results from Document Folder
Cocoa Pods Install on iOS Project Not Working
Uicollectionview Cells with Images Inside Uitableview Prototype
Swift and Nsuserdefaults - Exc_Bad_Instruction When User Defaults Empty
Type 'Any' Has No Subscript Members in Swift 3 Xcode 8
Ios11 Uibarbuttonitem Not Working
Audio Information of Current Track iOS Swift