Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native iOS Language Translated String and its Language Code Identification (LCID)

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

like image 549
aliasgar Avatar asked Dec 11 '22 19:12

aliasgar


2 Answers

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)")
}
like image 90
Martin R Avatar answered Jan 26 '23 00:01

Martin R


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.

like image 40
Paul Lalonde Avatar answered Jan 25 '23 22:01

Paul Lalonde