aliasgar
aliasgar

Reputation: 359

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.

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:

Upvotes: 4

Views: 2149

Answers (2)

Martin R
Martin R

Reputation: 539965

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)")
}

Upvotes: 11

Paul Lalonde
Paul Lalonde

Reputation: 5050

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.

Upvotes: 2

Related Questions