aboutsummaryrefslogtreecommitdiff
path: root/iPhone/GTMUILocalizer.m
blob: f9ec7528071b55339b522fc9c76f8d272500addb (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
//  GTMUILocalizer.m
//
//  Copyright 2011 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMDefines.h"
#import "GTMUILocalizer.h"

@interface GTMUILocalizer (GTMUILocalizerPrivate)
- (void)localizeAccessibility:(id)object;

// Never recursively call any of these methods. Always call
// -[self localizeObject:recursively:].
- (void)localizeToolbar:(UIToolbar *)toolbar;
- (void)localizeSegmentedControl:(UISegmentedControl *)segmentedControl;
- (void)localizeView:(UIView *)view recursively:(BOOL)recursive;
- (void)localizeButton:(UIButton *)button;
@end

@implementation GTMUILocalizer
@synthesize owner = owner_;
@synthesize otherObjectToLocalize = otherObjectToLocalize_;
@synthesize yetAnotherObjectToLocalize = yetAnotherObjectToLocalize_;

- (id)initWithBundle:(NSBundle *)bundle {
  if ((self = [super init])) {
    bundle_ = [bundle retain];
  }
  return self;
}

- (void)dealloc {
  [bundle_ release];
  [super dealloc];
}

- (void)awakeFromNib {
  [super awakeFromNib];
  id owner = self.owner;
  if (owner) {
    NSBundle *newBundle = [[self class] bundleForOwner:owner];
    bundle_ = [newBundle retain];
    [self localizeObject:self.owner recursively:YES];
    [self localizeObject:self.otherObjectToLocalize recursively:YES];
    [self localizeObject:self.yetAnotherObjectToLocalize recursively:YES];
  } else {
    _GTMDevLog(@"Expected an owner set for %@", self);
  }
  // Clear the outlets.
  self.owner = nil;
  self.otherObjectToLocalize = nil;
  self.yetAnotherObjectToLocalize = nil;
}

+ (NSBundle *)bundleForOwner:(id)owner {
  NSBundle *newBundle = nil;
  if (owner) {
    if ([owner isKindOfClass:[UIViewController class]]) {
      newBundle = [(UIViewController *)owner nibBundle];
    }
    if (!newBundle) {
      newBundle = [NSBundle mainBundle];
    }
  }
  return newBundle;
}

- (NSString *)localizedStringForString:(NSString *)string {
  NSString *localized = nil;
  if (bundle_ && [string hasPrefix:@"^"]) {
    NSString *notFoundValue = @"__GTM_NOT_FOUND__";
    NSString *key = [string substringFromIndex:1];
    localized = [bundle_ localizedStringForKey:key
                                         value:notFoundValue
                                         table:nil];
    if ([localized isEqualToString:notFoundValue]) {
      localized = nil;
    }
  }
  return localized;
}

- (void)localizeObject:(id)object recursively:(BOOL)recursive {
  if (object) {
    if ([object isKindOfClass:[UIViewController class]]) {
        UIView *view = [object view];
        [self localizeView:view recursively:recursive];
    } else if ([object isKindOfClass:[UIToolbar class]]) {
      [self localizeToolbar:(UIToolbar*)object];
    } else if ([object isKindOfClass:[UISegmentedControl class]]) {
      [self localizeSegmentedControl:(UISegmentedControl*)object];
    } else if ([object isKindOfClass:[UIView class]]) {
      [self localizeView:(UIView *)object recursively:recursive];
    }
  }
}

- (void)localizeToolbar:(UIToolbar *)toolbar {
  // NOTE: Like the header says, -items only gives us what is in the toolbar
  // which is usually the default items, if the toolbar supports customization
  // there is no way to fetch those possible items to tweak their contents.
  for (UIBarItem* item in [toolbar items]) {
    NSString *title = [item title];
    if (title) {
      title = [self localizedStringForString:title];
      if (title) {
        [item setTitle:title];
      }
    }
  }
}

- (void)localizeSegmentedControl:(UISegmentedControl *)segmentedControl {
  // A UISegmentedControl uses a few objects as subviews, but they aren't
  // documented.  It happened to work out that their inherritance was right
  // with the selectors they implemented that things localized, but iOS 6
  // changed some of that, so they are now directly handled.
  NSUInteger numberOfSegments = segmentedControl.numberOfSegments;
  for (NSUInteger i = 0; i < numberOfSegments; ++i) {
    NSString *title = [segmentedControl titleForSegmentAtIndex:i];
    if (title) {
      title = [self localizedStringForString:title];
      if (title) {
        [segmentedControl setTitle:title forSegmentAtIndex:i];
      }
    }
  }
}

- (void)localizeView:(UIView *)view recursively:(BOOL)recursive {
  if (view) {
    // Do accessibility on views.
    [self localizeAccessibility:view];

    if (recursive) {
      for (UIView *subview in [view subviews]) {
        [self localizeObject:subview recursively:recursive];
      }
    }

    // Specific types
    if ([view isKindOfClass:[UIButton class]]) {
      [self localizeButton:(UIButton *)view];
    }

    // Then do all possible strings.
    if ([view respondsToSelector:@selector(title)]
        && [view respondsToSelector:@selector(setTitle:)]) {
      NSString *title = [view performSelector:@selector(title)];
      if (title) {
        NSString *localizedTitle = [self localizedStringForString:title];
        if (localizedTitle) {
          [view performSelector:@selector(setTitle:) withObject:localizedTitle];
        }
      }
    }

    if ([view respondsToSelector:@selector(text)]
        && [view respondsToSelector:@selector(setText:)]) {
      NSString *text = [view performSelector:@selector(text)];
      if (text) {
        NSString *localizedText = [self localizedStringForString:text];
        if (localizedText) {
          [view performSelector:@selector(setText:) withObject:localizedText];
        }
      }
    }

    if ([view respondsToSelector:@selector(placeholder)]
        && [view respondsToSelector:@selector(setPlaceholder:)]) {
      NSString *placeholder = [view performSelector:@selector(placeholder)];
      if (placeholder) {
        NSString *localizedPlaceholder =
            [self localizedStringForString:placeholder];
        if (localizedPlaceholder) {
          [view performSelector:@selector(setPlaceholder:)
                     withObject:localizedPlaceholder];
        }
      }
    }
  }
}

- (void)localizeAccessibility:(id)object {
  if ([object respondsToSelector:@selector(accessibilityHint)]
      && [object respondsToSelector:@selector(setAccessibilityHint:)]) {
    NSString *accessibilityHint =
        [object performSelector:@selector(accessibilityHint)];
    if (accessibilityHint) {
      NSString *localizedAccessibilityHint =
          [self localizedStringForString:accessibilityHint];
      if (localizedAccessibilityHint) {
        [object performSelector:@selector(setAccessibilityHint:)
                   withObject:localizedAccessibilityHint];
      }
    }
  }

  if ([object respondsToSelector:@selector(accessibilityLabel)]
      && [object respondsToSelector:@selector(setAccessibilityLabel:)]) {
    NSString *accessibilityLabel =
        [object performSelector:@selector(accessibilityLabel)];
    if (accessibilityLabel) {
      NSString *localizedAccessibilityLabel =
          [self localizedStringForString:accessibilityLabel];
      if (localizedAccessibilityLabel) {
        [object performSelector:@selector(setAccessibilityLabel:)
                   withObject:localizedAccessibilityLabel];
      }
    }
  }

  if ([object respondsToSelector:@selector(accessibilityValue)]
      && [object respondsToSelector:@selector(setAccessibilityValue:)]) {
    NSString *accessibilityValue =
        [object performSelector:@selector(accessibilityValue)];
    if (accessibilityValue) {
      NSString *localizedAccessibilityValue =
          [self localizedStringForString:accessibilityValue];
      if (localizedAccessibilityValue) {
        [object performSelector:@selector(setAccessibilityValue:)
                   withObject:localizedAccessibilityValue];
      }
    }
  }
}

- (void)localizeButton:(UIButton *)button {
  UIControlState allStates[] = { UIControlStateNormal,
                                 UIControlStateHighlighted,
                                 UIControlStateDisabled,
                                 UIControlStateSelected };
  for (size_t idx = 0; idx < (sizeof(allStates)/sizeof(allStates[0])); ++idx) {
    UIControlState state = allStates[idx];
    NSString *value = [button titleForState:state];
    if (value) {
      NSString* localizedValue = [self localizedStringForString:value];
      if (localizedValue) {
        [button setTitle:localizedValue forState:state];
      }
    }
  }
}

@end