如何本地化数组中的所有字符串

时间:2010-05-25 05:28:25

标签: iphone objective-c cocoa cocoa-touch

我有一个动态的字符串数组,我想要本地化的元素。有没有办法在没有迭代的情况下本地化字符串,例如比如使用“makeObjectsPerformSelector”。谢谢

1 个答案:

答案 0 :(得分:2)

makeObjectsPerformSelector遍历数组。如果您想使用它而不是更快的迭代方法,请执行以下操作:

@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