Is there any way to insert an NSLocalizedString in interface builder. For example set a label text to a localized string instead of a static string?
I开发者_运维问答 really hate to create a property for every single item that requires a localized string.
This post might have some tips for you:
http://blog.wilshipley.com/2009/10/pimp-my-code-part-17-lost-in.html
Even if this post is old, for those interested in automatically localizing your IB files, check this out: https://github.com/angelolloqui/AGi18n
DISCLAIMER: I am the developer of the library
You can take advanced of the User Defined Runtime Attributes:
http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html
First define a new category for UILabel:
#import "UILabel+Localized.h"
@implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
[self setText:NSLocalizedString(aText, nil)];
}
@end
Then in the interface builder, User Defined Runtime Attributes :
textLocalized String your string to localized
To avoid creating a bunch of categories, create just one that categorize the NSObject and then check for the isKindOfClass as suggested. See the code below:
#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
NSLocalizedString is not the recommended way to localize Interface Builder files. Check out ibtool
:
http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/
I have done same thing as @OAK mentioned. Here is full code.
Interface Builder Localization HowTo
精彩评论