-
Notifications
You must be signed in to change notification settings - Fork 24
/
OHAutoNIBi18n.m
199 lines (163 loc) · 6.98 KB
/
OHAutoNIBi18n.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// OHAutoNIBi18n.m
//
// Created by Olivier on 03/11/10.
// Copyright 2010 FoodReporter. All rights reserved.
//
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
#import "OHAutoNIBi18n.h"
static inline NSString* localizedString(NSString* aString);
static inline void localizeUIBarButtonItem(UIBarButtonItem* bbi);
static inline void localizeUIBarItem(UIBarItem* bi);
static inline void localizeUIButton(UIButton* btn);
static inline void localizeUILabel(UILabel* lbl);
static inline void localizeUINavigationItem(UINavigationItem* ni);
static inline void localizeUISearchBar(UISearchBar* sb);
static inline void localizeUISegmentedControl(UISegmentedControl* sc);
static inline void localizeUITextField(UITextField* tf);
static inline void localizeUITextView(UITextView* tv);
static inline void localizeUIViewController(UIViewController* vc);
// ------------------------------------------------------------------------------------------------
static NSBundle* _bundle = nil;
static NSString* _tableName = nil;
@implementation OHAutoNIBi18n
+ (void)setLocalizationBundle:(NSBundle* __nullable)bundle tableName:(NSString* __nullable)tableName {
_bundle = bundle ?: [NSBundle mainBundle];
_tableName = tableName;
}
@end
@interface NSObject(OHAutoNIBi18n)
-(void)localizeNibObject;
@end
@implementation NSObject(OHAutoNIBi18n)
#define LocalizeIfClass(Cls) if ([self isKindOfClass:[Cls class]]) localize##Cls((Cls*)self)
-(void)localizeNibObject
{
LocalizeIfClass(UIBarButtonItem);
else LocalizeIfClass(UIBarItem);
else LocalizeIfClass(UIButton);
else LocalizeIfClass(UILabel);
else LocalizeIfClass(UINavigationItem);
else LocalizeIfClass(UISearchBar);
else LocalizeIfClass(UISegmentedControl);
else LocalizeIfClass(UITextField);
else LocalizeIfClass(UITextView);
else LocalizeIfClass(UIViewController);
if (![self isKindOfClass:UITableView.class] && (self.isAccessibilityElement == YES))
{
// Security to avoid translating tableView's accessibilityLabel & accessibilityHint
// since it seems to provoke a crash on some configurations
// See https://github.com/AliSoftware/OHAutoNIBi18n/issues/3
self.accessibilityLabel = localizedString(self.accessibilityLabel);
self.accessibilityHint = localizedString(self.accessibilityHint);
}
// Call the original awakeFromNib method
[self localizeNibObject]; // this actually calls the original awakeFromNib (and not localizeNibObject) because we did some method swizzling
}
+(void)load
{
// Autoload : swizzle -awakeFromNib with -localizeNibObject as soon as the app (and thus this class) is loaded
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method localizeNibObject = class_getInstanceMethod([NSObject class], @selector(localizeNibObject));
Method awakeFromNib = class_getInstanceMethod([NSObject class], @selector(awakeFromNib));
method_exchangeImplementations(awakeFromNib, localizeNibObject);
_bundle = [NSBundle mainBundle];
});
}
@end
/////////////////////////////////////////////////////////////////////////////
static inline NSString* localizedString(NSString* aString)
{
if (aString == nil || [aString length] == 0)
return aString;
// Don't translate strings starting with a digit
if ([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[aString characterAtIndex:0]])
return aString;
#if OHAutoNIBi18n_DEBUG
#warning Debug mode for i18n is active
static NSString* const kNoTranslation = @"$!";
NSString* tr = [_bundle localizedStringForKey:aString value:kNoTranslation table:_tableName];
if ([tr isEqualToString:kNoTranslation])
{
if ([aString hasPrefix:@"."])
{
// strings in XIB starting with '.' are typically used as temporary placeholder for design
// and will be replaced by code later, so don't warn about them
return aString;
}
NSLog(@"No translation for string '%@'",aString);
tr = [NSString stringWithFormat:@"$%@$",aString];
}
return tr;
#else
return [_bundle localizedStringForKey:aString value:nil table:_tableName];
#endif
}
// ------------------------------------------------------------------------------------------------
static inline void localizeUIBarButtonItem(UIBarButtonItem* bbi) {
localizeUIBarItem(bbi); /* inheritence */
NSMutableSet* locTitles = [[NSMutableSet alloc] initWithCapacity:[bbi.possibleTitles count]];
for(NSString* str in bbi.possibleTitles) {
[locTitles addObject:localizedString(str)];
}
bbi.possibleTitles = [NSSet setWithSet:locTitles];
#if ! __has_feature(objc_arc)
[locTitles release];
#endif
}
static inline void localizeUIBarItem(UIBarItem* bi) {
bi.title = localizedString(bi.title);
}
static inline void localizeUIButton(UIButton* btn) {
NSString* title[4] = {
[btn titleForState:UIControlStateNormal],
[btn titleForState:UIControlStateHighlighted],
[btn titleForState:UIControlStateDisabled],
[btn titleForState:UIControlStateSelected]
};
[btn setTitle:localizedString(title[0]) forState:UIControlStateNormal];
if (title[1] == [btn titleForState:UIControlStateHighlighted])
[btn setTitle:localizedString(title[1]) forState:UIControlStateHighlighted];
if (title[2] == [btn titleForState:UIControlStateDisabled])
[btn setTitle:localizedString(title[2]) forState:UIControlStateDisabled];
if (title[3] == [btn titleForState:UIControlStateSelected])
[btn setTitle:localizedString(title[3]) forState:UIControlStateSelected];
}
static inline void localizeUILabel(UILabel* lbl) {
lbl.text = localizedString(lbl.text);
}
static inline void localizeUINavigationItem(UINavigationItem* ni) {
ni.title = localizedString(ni.title);
ni.prompt = localizedString(ni.prompt);
}
static inline void localizeUISearchBar(UISearchBar* sb) {
sb.placeholder = localizedString(sb.placeholder);
sb.prompt = localizedString(sb.prompt);
sb.text = localizedString(sb.text);
NSMutableArray* locScopesTitles = [[NSMutableArray alloc] initWithCapacity:[sb.scopeButtonTitles count]];
for(NSString* str in sb.scopeButtonTitles) {
[locScopesTitles addObject:localizedString(str)];
}
sb.scopeButtonTitles = [NSArray arrayWithArray:locScopesTitles];
#if ! __has_feature(objc_arc)
[locScopesTitles release];
#endif
}
static inline void localizeUISegmentedControl(UISegmentedControl* sc) {
NSUInteger n = sc.numberOfSegments;
for(NSUInteger idx = 0; idx<n; ++idx) {
[sc setTitle:localizedString([sc titleForSegmentAtIndex:idx]) forSegmentAtIndex:idx];
}
}
static inline void localizeUITextField(UITextField* tf) {
tf.text = localizedString(tf.text);
tf.placeholder = localizedString(tf.placeholder);
}
static inline void localizeUITextView(UITextView* tv) {
tv.text = localizedString(tv.text);
}
static inline void localizeUIViewController(UIViewController* vc) {
vc.title = localizedString(vc.title);
}