Check Language in iOS App

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":

Sample Image



Related Topics



Leave a reply



Submit