有没有一种方法可以在界面构建器中插入NSLocalizedString。 例如将标签文本设置为本地化字符串而不是静态字符串?
我真的不想为每个需要本地化字符串的项目创建一个属性。
有没有一种方法可以在界面构建器中插入NSLocalizedString。 例如将标签文本设置为本地化字符串而不是静态字符串?
我真的不想为每个需要本地化字符串的项目创建一个属性。
#import "NSObject+Localized.h"
@implementation NSObject (Localized)
///
/// This method is used to translate strings in .xib files.
/// Using the "User Defined Runtime Attributes" set an entry like:
/// Key Path: textLocalized
/// Type: String
/// Value: {THE TRANSLATION KEY}
///
-(void) setTextLocalized:(NSString *)key
{
if ([self isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)self;
[label setText:NSLocalizedString(key, nil)];
}
else if ([self isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)self;
[button setTitle:NSLocalizedString(key, nil) forState:UIControlStateNormal];
}
else if ([self isKindOfClass:[UIBarButtonItem class]])
{
UIBarButtonItem *button = (UIBarButtonItem *)self;
[button setTitle:NSLocalizedString(key, nil)];
}
}
@end
#import "UILabel+Localized.h"
@implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
[self setText:NSLocalizedString(aText, nil)];
}
@end
然后在界面构建器中,选择“用户定义的运行时属性”:
textLocalized String 你要本地化的字符串
NSLocalizedString不是本地化Interface Builder文件的推荐方式。请查看ibtool
:
http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/