开发者

How to localize all strings in an array

开发者 https://www.devze.com 2022-12-31 18:05 出处:网络
I have a dynamic array of strings, the elements of which I would l开发者_JS百科ike to localize. Is there a way to localize the strings without iteration e.g. something like using \"makeObjectsPerformS

I have a dynamic array of strings, the elements of which I would l开发者_JS百科ike to localize. Is there a way to localize the strings without iteration e.g. something like using "makeObjectsPerformSelector". Thank you


makeObjectsPerformSelector iterates through the array. If you want to use that instead of the faster method of iterating yourself, do this:

@interface NSString (MyCategory)
-(void) localizeToArray:(NSMutableArray *)ioArray;
@end

@implementation NSString (MyCategory)
-(void) localizeToArray:(NSMutableArray *)ioArray {
  [ioArray addObject:[[NSBundle mainBundle] localizedStringForKey:self value:self table:nil]];
}
@end

@interface NSArray (MyCategory)
-(NSArray *) arrayWithLocalizedStrings;
@end

@implementation NSArray (MyCategory)
-(NSArray *) arrayWithLocalizedStrings {
  NSMutableArray *result = [NSMutableArray arrayWithCapacity:[self count]];
  [self makeObjectsPerformSelector:@selector(localizeToArray:) withObject:result];
  return result;
}
@end
0

精彩评论

暂无评论...
验证码 换一张
取 消