Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Last active December 11, 2015 04:29
Show Gist options
  • Save swissmanu/4545425 to your computer and use it in GitHub Desktop.
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 :-)
NSString* localizedProperty(NSString* propertyName, id fromObject);
#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