PageRenderTime 68ms CodeModel.GetById 1ms app.highlight 63ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/iPhone/GTMUILocalizer.m

http://macfuse.googlecode.com/
Objective C | 257 lines | 204 code | 23 blank | 30 comment | 54 complexity | ec45369da95e867c52a283aad488bd20 MD5 | raw file
  1//
  2//  GTMUILocalizer.m
  3//
  4//  Copyright 2011 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMDefines.h"
 20#import "GTMUILocalizer.h"
 21
 22@interface GTMUILocalizer (GTMUILocalizerPrivate)
 23- (void)localizeAccessibility:(id)object;
 24
 25// Never recursively call any of these methods. Always call
 26// -[self localizeObject:recursively:].
 27- (void)localizeToolbar:(UIToolbar *)toolbar;
 28- (void)localizeSegmentedControl:(UISegmentedControl *)segmentedControl;
 29- (void)localizeView:(UIView *)view recursively:(BOOL)recursive;
 30- (void)localizeButton:(UIButton *)button;
 31@end
 32
 33@implementation GTMUILocalizer
 34@synthesize owner = owner_;
 35@synthesize otherObjectToLocalize = otherObjectToLocalize_;
 36@synthesize yetAnotherObjectToLocalize = yetAnotherObjectToLocalize_;
 37
 38- (id)initWithBundle:(NSBundle *)bundle {
 39  if ((self = [super init])) {
 40    bundle_ = [bundle retain];
 41  }
 42  return self;
 43}
 44
 45- (void)dealloc {
 46  [bundle_ release];
 47  [super dealloc];
 48}
 49
 50- (void)awakeFromNib {
 51  id owner = self.owner;
 52  if (owner) {
 53    NSBundle *newBundle = [[self class] bundleForOwner:owner];
 54    bundle_ = [newBundle retain];
 55    [self localizeObject:self.owner recursively:YES];
 56    [self localizeObject:self.otherObjectToLocalize recursively:YES];
 57    [self localizeObject:self.yetAnotherObjectToLocalize recursively:YES];
 58  } else {
 59    _GTMDevLog(@"Expected an owner set for %@", self);
 60  }
 61  // Clear the outlets.
 62  self.owner = nil;
 63  self.otherObjectToLocalize = nil;
 64  self.yetAnotherObjectToLocalize = nil;
 65}
 66
 67+ (NSBundle *)bundleForOwner:(id)owner {
 68  NSBundle *newBundle = nil;
 69  if (owner) {
 70    if ([owner isKindOfClass:[UIViewController class]]) {
 71      newBundle = [(UIViewController *)owner nibBundle];
 72    }
 73    if (!newBundle) {
 74      newBundle = [NSBundle mainBundle];
 75    }
 76  }
 77  return newBundle;
 78}
 79
 80- (NSString *)localizedStringForString:(NSString *)string {
 81  NSString *localized = nil;
 82  if (bundle_ && [string hasPrefix:@"^"]) {
 83    NSString *notFoundValue = @"__GTM_NOT_FOUND__";
 84    NSString *key = [string substringFromIndex:1];
 85    localized = [bundle_ localizedStringForKey:key
 86                                         value:notFoundValue
 87                                         table:nil];
 88    if ([localized isEqualToString:notFoundValue]) {
 89      localized = nil;
 90    }
 91  }
 92  return localized;
 93}
 94
 95- (void)localizeObject:(id)object recursively:(BOOL)recursive {
 96  if (object) {
 97    if ([object isKindOfClass:[UIViewController class]]) {
 98        UIView *view = [object view];
 99        [self localizeView:view recursively:recursive];
100    } else if ([object isKindOfClass:[UIToolbar class]]) {
101      [self localizeToolbar:(UIToolbar*)object];
102    } else if ([object isKindOfClass:[UISegmentedControl class]]) {
103      [self localizeSegmentedControl:(UISegmentedControl*)object];
104    } else if ([object isKindOfClass:[UIView class]]) {
105      [self localizeView:(UIView *)object recursively:recursive];
106    }
107  }
108}
109
110- (void)localizeToolbar:(UIToolbar *)toolbar {
111  // NOTE: Like the header says, -items only gives us what is in the toolbar
112  // which is usually the default items, if the toolbar supports customization
113  // there is no way to fetch those possible items to tweak their contents.
114  for (UIBarItem* item in [toolbar items]) {
115    NSString *title = [item title];
116    if (title) {
117      title = [self localizedStringForString:title];
118      if (title) {
119        [item setTitle:title];
120      }
121    }
122  }
123}
124
125- (void)localizeSegmentedControl:(UISegmentedControl *)segmentedControl {
126  // A UISegmentedControl uses a few objects as subviews, but they aren't
127  // documented.  It happened to work out that their inherritance was right
128  // with the selectors they implemented that things localized, but iOS 6
129  // changed some of that, so they are now directly handled.
130  NSUInteger numberOfSegments = segmentedControl.numberOfSegments;
131  for (NSUInteger i = 0; i < numberOfSegments; ++i) {
132    NSString *title = [segmentedControl titleForSegmentAtIndex:i];
133    if (title) {
134      title = [self localizedStringForString:title];
135      if (title) {
136        [segmentedControl setTitle:title forSegmentAtIndex:i];
137      }
138    }
139  }
140}
141
142- (void)localizeView:(UIView *)view recursively:(BOOL)recursive {
143  if (view) {
144    // Do accessibility on views.
145    [self localizeAccessibility:view];
146
147    if (recursive) {
148      for (UIView *subview in [view subviews]) {
149        [self localizeObject:subview recursively:recursive];
150      }
151    }
152
153    // Specific types
154    if ([view isKindOfClass:[UIButton class]]) {
155      [self localizeButton:(UIButton *)view];
156    }
157
158    // Then do all possible strings.
159    if ([view respondsToSelector:@selector(title)]
160        && [view respondsToSelector:@selector(setTitle:)]) {
161      NSString *title = [view performSelector:@selector(title)];
162      if (title) {
163        NSString *localizedTitle = [self localizedStringForString:title];
164        if (localizedTitle) {
165          [view performSelector:@selector(setTitle:) withObject:localizedTitle];
166        }
167      }
168    }
169
170    if ([view respondsToSelector:@selector(text)]
171        && [view respondsToSelector:@selector(setText:)]) {
172      NSString *text = [view performSelector:@selector(text)];
173      if (text) {
174        NSString *localizedText = [self localizedStringForString:text];
175        if (localizedText) {
176          [view performSelector:@selector(setText:) withObject:localizedText];
177        }
178      }
179    }
180
181    if ([view respondsToSelector:@selector(placeholder)]
182        && [view respondsToSelector:@selector(setPlaceholder:)]) {
183      NSString *placeholder = [view performSelector:@selector(placeholder)];
184      if (placeholder) {
185        NSString *localizedPlaceholder =
186            [self localizedStringForString:placeholder];
187        if (localizedPlaceholder) {
188          [view performSelector:@selector(setPlaceholder:)
189                     withObject:localizedPlaceholder];
190        }
191      }
192    }
193  }
194}
195
196- (void)localizeAccessibility:(id)object {
197  if ([object respondsToSelector:@selector(accessibilityHint)]
198      && [object respondsToSelector:@selector(setAccessibilityHint:)]) {
199    NSString *accessibilityHint =
200        [object performSelector:@selector(accessibilityHint)];
201    if (accessibilityHint) {
202      NSString *localizedAccessibilityHint =
203          [self localizedStringForString:accessibilityHint];
204      if (localizedAccessibilityHint) {
205        [object performSelector:@selector(setAccessibilityHint:)
206                   withObject:localizedAccessibilityHint];
207      }
208    }
209  }
210
211  if ([object respondsToSelector:@selector(accessibilityLabel)]
212      && [object respondsToSelector:@selector(setAccessibilityLabel:)]) {
213    NSString *accessibilityLabel =
214        [object performSelector:@selector(accessibilityLabel)];
215    if (accessibilityLabel) {
216      NSString *localizedAccessibilityLabel =
217          [self localizedStringForString:accessibilityLabel];
218      if (localizedAccessibilityLabel) {
219        [object performSelector:@selector(setAccessibilityLabel:)
220                   withObject:localizedAccessibilityLabel];
221      }
222    }
223  }
224
225  if ([object respondsToSelector:@selector(accessibilityValue)]
226      && [object respondsToSelector:@selector(setAccessibilityValue:)]) {
227    NSString *accessibilityValue =
228        [object performSelector:@selector(accessibilityValue)];
229    if (accessibilityValue) {
230      NSString *localizedAccessibilityValue =
231          [self localizedStringForString:accessibilityValue];
232      if (localizedAccessibilityValue) {
233        [object performSelector:@selector(setAccessibilityValue:)
234                   withObject:localizedAccessibilityValue];
235      }
236    }
237  }
238}
239
240- (void)localizeButton:(UIButton *)button {
241  UIControlState allStates[] = { UIControlStateNormal,
242                                 UIControlStateHighlighted,
243                                 UIControlStateDisabled,
244                                 UIControlStateSelected };
245  for (size_t idx = 0; idx < (sizeof(allStates)/sizeof(allStates[0])); ++idx) {
246    UIControlState state = allStates[idx];
247    NSString *value = [button titleForState:state];
248    if (value) {
249      NSString* localizedValue = [self localizedStringForString:value];
250      if (localizedValue) {
251        [button setTitle:localizedValue forState:state];
252      }
253    }
254  }
255}
256
257@end