5

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?

XY L
  • 25,431
  • 14
  • 84
  • 143

1 Answers1

19

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") // "ポーランド ズウォティ"
Witek Bobrowski
  • 3,749
  • 1
  • 20
  • 34