How to get the real app language code + variation?

Hello,

I have the app localised to many languages and its variants, like English from Australia or Dutch from Belgium. My phone language is en_UK and the region is configured to ES.

I cannot find a way to get the regional language code (ex: en_au, nl_be), it always gets simplified to the language (ex: en, nl), or adds my device region (ex: en-ES, nl-ES).

Here I attach some examples while I have the app on en_au

Locale.preferredLanguages
▿ 3 elements
  - 0 : "en-ES"
  - 1 : "ca-ES"
  - 2 : "es-ES"

Bundle.main.preferredLocalizations
▿ 1 element
  - 0 : "en"

NSLocale.current.languageCode
▿ Optional<String>
  - some : "en"

Locale.current
▿ en_001@rg=eszzzz (fixed en_001@rg=eszzzz)
  - identifier : "en_001@rg=eszzzz"
  - locale : "fixed en_001@rg=eszzzz"

Locale.autoupdatingCurrent.languageCode
▿ Optional<String>
  - some : "en"

Locale.current.languageCode
▿ Optional<String>
  - some : "en"

Locale.preferredLanguages[0]
"en-ES"

I tried with my iPhone +iOS 18.2.1 with the following configuration, and the system does seem to respect the system language and per-app language settings:

The available localizations in my app bundle:

  • en.lproj
  • en-AU.lproj
  • zh-Hans.lproj

The preferred languages (Settings > General > language & Region):

  1. English
  2. Chinese, Simplified
  3. English (Australia)

The Region is United States.

The per-app language (Settings > Apps > My app > Language):

  1. English
  2. Chinese, Simplified
  3. English (Australia) ✓

Note that English (Australia) is checked.

When running the app with Xcode, I see that en-AU appears as the first item:

Locale.preferredLanguages = ["en-AU", "en-US", "zh-Hans-US"]
Bundle.main.preferredLocalizations = ["en-AU", "en"]

(I can't explain why zh-Hans is missed in Bundle.main.preferredLocalizations, but typically only the first one matters.)

I am wondering if you use your own way, rather than the .lproj folders, to organize your localizations... If you can provide a minimal project that contains only the code relevant to the issue, with detailed steps to reproduce the issue, I'd be interested in taking a look.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Try Locale.current.language

How to get the real app language code + variation?
 
 
Q