Skip to content

Instantly share code, notes, and snippets.

@matthiaswenz
Last active December 13, 2015 22:19
Show Gist options
  • Save matthiaswenz/4984002 to your computer and use it in GitHub Desktop.
Save matthiaswenz/4984002 to your computer and use it in GitHub Desktop.
A simple category on NSDictionary which allows localized values for string-based keys, built for @bowstreet. License: MIT - go on, have fun with it, but don't expect anything from us. You might ask politely, though - we're a nice bunch.
#import <Foundation/Foundation.h>
@interface NSDictionary (Localized)
- (id)localizedValueForKey:(NSString *)aKey;
- (id)localizedValueForKey:(NSString *)aKey withLanguage:(NSString *)aLanguage;
@end
#import "NSDictionary+Localized.h"
@implementation NSDictionary (Localized)
static NSString *defaultLanguage = @"en";
- (id)localizedValueForKey:(NSString *)aKey {
return [self localizedValueForKey:aKey withLanguage:defaultLanguage];
}
- (id)localizedValueForKey:(NSString *)aKey withLanguage:(NSString *)aLanguage {
id value = [self valueForKey:[NSString stringWithFormat:@"%@_%@", aKey, aLanguage]];
if (value != nil) {
return value;
}
return [self valueForKey:aKey];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment