Last active
September 26, 2017 16:34
-
-
Save dneprDroid/5922f6436f24aceaafaf145d20809a44 to your computer and use it in GitHub Desktop.
IBInspectable Localization in Storyboards.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK : Localization+IBInspectable.h | |
@interface UILabel(Localization) | |
@property (nonatomic, weak) IBInspectable NSString *localizationId; | |
@end | |
@interface UIButton(Localization) | |
@property (nonatomic, weak) IBInspectable NSString *localizationId; | |
@end | |
@interface UITextField(Localization) | |
@property (nonatomic, weak) IBInspectable NSString *placeholderLocalizationId; | |
@end | |
@interface UITextView(Localization) | |
@property (nonatomic, weak) IBInspectable NSString *localizationId; | |
@end | |
// MARK : Localization+IBInspectable.m | |
NS_INLINE BOOL isEngLang() { | |
NSString* language = [[NSLocale preferredLanguages] objectAtIndex:0]; | |
return [[language lowercaseString] hasPrefix:@"en"]; | |
} | |
NS_INLINE NSAttributedString* formatCharXMLTagsUsingAttributes(NSString *string, | |
UIFont *font, UIColor *textColor){ | |
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, | |
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}; | |
NSMutableAttributedString *mutAttrString = [[NSMutableAttributedString alloc] initWithData:[string dataUsingEncoding:NSUTF8StringEncoding] | |
options: options | |
documentAttributes: nil error:nil]; | |
[mutAttrString addAttribute:NSFontAttributeName value:font range: NSMakeRange(0, mutAttrString.string.length)]; | |
[mutAttrString addAttribute:NSForegroundColorAttributeName value:textColor range: NSMakeRange(0, mutAttrString.string.length)]; | |
return mutAttrString; | |
} | |
@implementation UILabel(Localization) | |
- (void)setLocalizationId:(NSString *)localizationId { | |
if (localizationId.length == 0 || isEngLang()) | |
return; | |
NSString *localized = NSLocalizedString(localizationId, nil); | |
if (localized.length == 0) { | |
return; | |
} | |
self.text = localized; | |
} | |
- (NSString *)localizationId { | |
return @""; | |
} | |
@end | |
@implementation UIButton(Localization) | |
- (void)setLocalizationId:(NSString *)localizationId { | |
if (localizationId.length == 0 || isEngLang()) | |
return; | |
NSString *localized = NSLocalizedString(localizationId, nil); | |
if (localized.length == 0) { | |
return; | |
} | |
if (self.currentAttributedTitle.string.length > 0) { | |
NSAttributedString *attrString = formatCharXMLTagsUsingAttributes(localized, | |
self.titleLabel.font, | |
self.titleLabel.textColor); | |
[self setAttributedTitle: attrString forState:UIControlStateNormal]; | |
} else { | |
[self setTitle: localized forState: UIControlStateNormal]; | |
} | |
} | |
- (NSString *)localizationId { | |
return @""; | |
} | |
@end | |
@implementation UITextView(Localization) | |
- (void)setLocalizationId:(NSString *)localizationId { | |
if (localizationId.length == 0 || isEngLang()) | |
return; | |
NSString *localized = NSLocalizedString(localizationId, nil); | |
if (localized.length == 0) { | |
return; | |
} | |
if (self.attributedText.string.length > 0) { | |
self.attributedText = formatCharXMLTagsUsingAttributes(localized, self.font, self.textColor); | |
} else { | |
self.text = localized; | |
} | |
} | |
- (NSString *)localizationId { | |
return @""; | |
} | |
@end | |
@implementation UITextField(Localization) | |
- (void)setPlaceholderLocalizationId:(NSString *)placeholderLocalizationId { | |
if (placeholderLocalizationId == 0 || isEngLang()) | |
return; | |
NSString *localized = NSLocalizedString(placeholderLocalizationId, nil); | |
if (localized.length == 0) | |
return; | |
self.placeholder = localized; | |
} | |
- (NSString *)placeholderLocalizationId { | |
return @""; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment