Last active
December 11, 2015 04:29
-
-
Save swissmanu/4545425 to your computer and use it in GitHub Desktop.
Assuming you have a crappy model which contains properties with a locale identifier as suffix (like "title_de", "title_en" etc), `localizedProperty(NSString*, id)` returns a specific translation of that property. This Gist provides a global function and is ready to use :-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString* localizedProperty(NSString* propertyName, id fromObject); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Helpers.h" | |
NSString* localizedProperty(NSString *propertyName, id fromObject) { | |
NSString *locale = [[NSLocale preferredLanguages] objectAtIndex:0]; | |
NSString *value = @""; | |
NSMutableString *localizedPropertyName = [NSMutableString stringWithString:propertyName]; | |
[localizedPropertyName appendString:@"_"]; | |
[localizedPropertyName appendString:locale]; | |
SEL getter = NSSelectorFromString(localizedPropertyName); | |
if([object respondsToSelector:getter]) { | |
value = [object valueForKey:localizedPropertyName]; | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment