How to get human friendly current local language name in iOS?

Viewed 870

The current language code can be retrieved via Locale.current.languageCode but what if I need a more human-friendly name like "English" or "Japanese"?

Does the developer must maintain a mapping in the app or there is a native way to get the language name?

1 Answers

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") // "ポーランド ズウォティ"
Related