Question

How does iOS Show the List of Languages in the Translated String or in the Language's Locale.

  • Is there an LCID associated with it?
  • If so, where can I find it's Mapping?
  • How does it work?
  • Is there an API or any documentation you can point me towards?

I am attaching a screenshot of what I actually mean: enter image description here

All I could find is this link

Update: I want to achieve something like this:

  • Where I can map the Country with its Language, which is actually in its Locale. So is there an LCID Kind of a thing where I can map it and get that Locale string using the LCID using an iOS API?

  • Mockup is Below
    enter image description here

Était-ce utile?

La solution

You can get a list of languages translated in the languages locale with the following code:

Objective-C:

NSArray *languages = [NSLocale preferredLanguages];
for (NSString *lang in languages)
{
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:lang];
    NSString *translated = [locale displayNameForKey:NSLocaleIdentifier value:lang];
    NSLog(@"%@, %@", lang, translated);
}

Swift:

let languages = NSLocale.preferredLanguages()
for lang in languages {
    let locale = NSLocale(localeIdentifier: lang)
    let translated = locale.displayNameForKey(NSLocaleIdentifier, value: lang)!
    print("\(lang), \(translated)")
}

Output:

en, English
fr, français
de, Deutsch
ja, 日本語
nl, Nederlands
...

I hope that this answers the first part of your question and perhaps helps with the second part.


Swift 3 update:

let languages = NSLocale.preferredLanguages
for lang in languages {
    let locale = NSLocale(localeIdentifier: lang)
    let translated = locale.displayName(forKey: NSLocale.Key.identifier, value: lang)!
    print("\(lang), \(translated)")
}

Autres conseils

LCIDs are a Windows concept. There is no such thing on iOS. Languages are generally identified by ISO 639-1 or 639-2 language codes.

From the way you frame your question, I think it would be best to start by reading Apple's internationalization and localization documentation. NSLocale is going to be your friend.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top