Skip to content

Instantly share code, notes, and snippets.

@ipedro
Last active January 8, 2016 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipedro/645d202d48649fdc196c to your computer and use it in GitHub Desktop.
Save ipedro/645d202d48649fdc196c to your computer and use it in GitHub Desktop.
extract country display names in different languages
-(void)makeCountriesJSON
{
NSArray<NSString*> *localeIds = @[@"pt", @"en"];
NSArray<NSString*> *countryCodesArray = [NSLocale ISOCountryCodes];
NSMutableString *json = [NSMutableString stringWithString:@"{"];
[localeIds enumerateObjectsUsingBlock:^(NSString * localeId, NSUInteger localeIndex, BOOL * _Nonnull stop) {
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:localeId];
[json appendFormat:@"\"%@\": {", locale.localeIdentifier];
[countryCodesArray enumerateObjectsUsingBlock:^(NSString *code, NSUInteger codeIndex, BOOL * _Nonnull stop) {
[json appendFormat:@"\"%@\":\"%@\"", code, [locale displayNameForKey:NSLocaleCountryCode value:code]];
if (codeIndex < countryCodesArray.count-1)
{
[json appendString:@","];
}
}];
[json appendString:@"}"];
if (localeIndex < localeIds.count-1)
{
[json appendString:@","];
}
}];
[json appendString:@"}"];
NSLog(@"%@", json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment