/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. #import "GTMDefines.h"
  19. #import "GTMUILocalizer.h"
  20. @interface GTMUILocalizer (GTMUILocalizerPrivate)
  21. - (void)localizeAccessibility:(id)object;
  22. // Never recursively call any of these methods. Always call
  23. // -[self localizeObject:recursively:].
  24. - (void)localizeToolbar:(UIToolbar *)toolbar;
  25. - (void)localizeSegmentedControl:(UISegmentedControl *)segmentedControl;
  26. - (void)localizeView:(UIView *)view recursively:(BOOL)recursive;
  27. - (void)localizeButton:(UIButton *)button;
  28. @end
  29. @implementation GTMUILocalizer
  30. @synthesize owner = owner_;
  31. @synthesize otherObjectToLocalize = otherObjectToLocalize_;
  32. @synthesize yetAnotherObjectToLocalize = yetAnotherObjectToLocalize_;
  33. - (id)initWithBundle:(NSBundle *)bundle {
  34. if ((self = [super init])) {
  35. bundle_ = [bundle retain];
  36. }
  37. return self;
  38. }
  39. - (void)dealloc {
  40. [bundle_ release];
  41. [super dealloc];
  42. }
  43. - (void)awakeFromNib {
  44. id owner = self.owner;
  45. if (owner) {
  46. NSBundle *newBundle = [[self class] bundleForOwner:owner];
  47. bundle_ = [newBundle retain];
  48. [self localizeObject:self.owner recursively:YES];
  49. [self localizeObject:self.otherObjectToLocalize recursively:YES];
  50. [self localizeObject:self.yetAnotherObjectToLocalize recursively:YES];
  51. } else {
  52. _GTMDevLog(@"Expected an owner set for %@", self);
  53. }
  54. // Clear the outlets.
  55. self.owner = nil;
  56. self.otherObjectToLocalize = nil;
  57. self.yetAnotherObjectToLocalize = nil;
  58. }
  59. + (NSBundle *)bundleForOwner:(id)owner {
  60. NSBundle *newBundle = nil;
  61. if (owner) {
  62. if ([owner isKindOfClass:[UIViewController class]]) {
  63. newBundle = [(UIViewController *)owner nibBundle];
  64. }
  65. if (!newBundle) {
  66. newBundle = [NSBundle mainBundle];
  67. }
  68. }
  69. return newBundle;
  70. }
  71. - (NSString *)localizedStringForString:(NSString *)string {
  72. NSString *localized = nil;
  73. if (bundle_ && [string hasPrefix:@"^"]) {
  74. NSString *notFoundValue = @"__GTM_NOT_FOUND__";
  75. NSString *key = [string substringFromIndex:1];
  76. localized = [bundle_ localizedStringForKey:key
  77. value:notFoundValue
  78. table:nil];
  79. if ([localized isEqualToString:notFoundValue]) {
  80. localized = nil;
  81. }
  82. }
  83. return localized;
  84. }
  85. - (void)localizeObject:(id)object recursively:(BOOL)recursive {
  86. if (object) {
  87. if ([object isKindOfClass:[UIViewController class]]) {
  88. UIView *view = [object view];
  89. [self localizeView:view recursively:recursive];
  90. } else if ([object isKindOfClass:[UIToolbar class]]) {
  91. [self localizeToolbar:(UIToolbar*)object];
  92. } else if ([object isKindOfClass:[UISegmentedControl class]]) {
  93. [self localizeSegmentedControl:(UISegmentedControl*)object];
  94. } else if ([object isKindOfClass:[UIView class]]) {
  95. [self localizeView:(UIView *)object recursively:recursive];
  96. }
  97. }
  98. }
  99. - (void)localizeToolbar:(UIToolbar *)toolbar {
  100. // NOTE: Like the header says, -items only gives us what is in the toolbar
  101. // which is usually the default items, if the toolbar supports customization
  102. // there is no way to fetch those possible items to tweak their contents.
  103. for (UIBarItem* item in [toolbar items]) {
  104. NSString *title = [item title];
  105. if (title) {
  106. title = [self localizedStringForString:title];
  107. if (title) {
  108. [item setTitle:title];
  109. }
  110. }
  111. }
  112. }
  113. - (void)localizeSegmentedControl:(UISegmentedControl *)segmentedControl {
  114. // A UISegmentedControl uses a few objects as subviews, but they aren't
  115. // documented. It happened to work out that their inherritance was right
  116. // with the selectors they implemented that things localized, but iOS 6
  117. // changed some of that, so they are now directly handled.
  118. NSUInteger numberOfSegments = segmentedControl.numberOfSegments;
  119. for (NSUInteger i = 0; i < numberOfSegments; ++i) {
  120. NSString *title = [segmentedControl titleForSegmentAtIndex:i];
  121. if (title) {
  122. title = [self localizedStringForString:title];
  123. if (title) {
  124. [segmentedControl setTitle:title forSegmentAtIndex:i];
  125. }
  126. }
  127. }
  128. }
  129. - (void)localizeView:(UIView *)view recursively:(BOOL)recursive {
  130. if (view) {
  131. // Do accessibility on views.
  132. [self localizeAccessibility:view];
  133. if (recursive) {
  134. for (UIView *subview in [view subviews]) {
  135. [self localizeObject:subview recursively:recursive];
  136. }
  137. }
  138. // Specific types
  139. if ([view isKindOfClass:[UIButton class]]) {
  140. [self localizeButton:(UIButton *)view];
  141. }
  142. // Then do all possible strings.
  143. if ([view respondsToSelector:@selector(title)]
  144. && [view respondsToSelector:@selector(setTitle:)]) {
  145. NSString *title = [view performSelector:@selector(title)];
  146. if (title) {
  147. NSString *localizedTitle = [self localizedStringForString:title];
  148. if (localizedTitle) {
  149. [view performSelector:@selector(setTitle:) withObject:localizedTitle];
  150. }
  151. }
  152. }
  153. if ([view respondsToSelector:@selector(text)]
  154. && [view respondsToSelector:@selector(setText:)]) {
  155. NSString *text = [view performSelector:@selector(text)];
  156. if (text) {
  157. NSString *localizedText = [self localizedStringForString:text];
  158. if (localizedText) {
  159. [view performSelector:@selector(setText:) withObject:localizedText];
  160. }
  161. }
  162. }
  163. if ([view respondsToSelector:@selector(placeholder)]
  164. && [view respondsToSelector:@selector(setPlaceholder:)]) {
  165. NSString *placeholder = [view performSelector:@selector(placeholder)];
  166. if (placeholder) {
  167. NSString *localizedPlaceholder =
  168. [self localizedStringForString:placeholder];
  169. if (localizedPlaceholder) {
  170. [view performSelector:@selector(setPlaceholder:)
  171. withObject:localizedPlaceholder];
  172. }
  173. }
  174. }
  175. }
  176. }
  177. - (void)localizeAccessibility:(id)object {
  178. if ([object respondsToSelector:@selector(accessibilityHint)]
  179. && [object respondsToSelector:@selector(setAccessibilityHint:)]) {
  180. NSString *accessibilityHint =
  181. [object performSelector:@selector(accessibilityHint)];
  182. if (accessibilityHint) {
  183. NSString *localizedAccessibilityHint =
  184. [self localizedStringForString:accessibilityHint];
  185. if (localizedAccessibilityHint) {
  186. [object performSelector:@selector(setAccessibilityHint:)
  187. withObject:localizedAccessibilityHint];
  188. }
  189. }
  190. }
  191. if ([object respondsToSelector:@selector(accessibilityLabel)]
  192. && [object respondsToSelector:@selector(setAccessibilityLabel:)]) {
  193. NSString *accessibilityLabel =
  194. [object performSelector:@selector(accessibilityLabel)];
  195. if (accessibilityLabel) {
  196. NSString *localizedAccessibilityLabel =
  197. [self localizedStringForString:accessibilityLabel];
  198. if (localizedAccessibilityLabel) {
  199. [object performSelector:@selector(setAccessibilityLabel:)
  200. withObject:localizedAccessibilityLabel];
  201. }
  202. }
  203. }
  204. if ([object respondsToSelector:@selector(accessibilityValue)]
  205. && [object respondsToSelector:@selector(setAccessibilityValue:)]) {
  206. NSString *accessibilityValue =
  207. [object performSelector:@selector(accessibilityValue)];
  208. if (accessibilityValue) {
  209. NSString *localizedAccessibilityValue =
  210. [self localizedStringForString:accessibilityValue];
  211. if (localizedAccessibilityValue) {
  212. [object performSelector:@selector(setAccessibilityValue:)
  213. withObject:localizedAccessibilityValue];
  214. }
  215. }
  216. }
  217. }
  218. - (void)localizeButton:(UIButton *)button {
  219. UIControlState allStates[] = { UIControlStateNormal,
  220. UIControlStateHighlighted,
  221. UIControlStateDisabled,
  222. UIControlStateSelected };
  223. for (size_t idx = 0; idx < (sizeof(allStates)/sizeof(allStates[0])); ++idx) {
  224. UIControlState state = allStates[idx];
  225. NSString *value = [button titleForState:state];
  226. if (value) {
  227. NSString* localizedValue = [self localizedStringForString:value];
  228. if (localizedValue) {
  229. [button setTitle:localizedValue forState:state];
  230. }
  231. }
  232. }
  233. }
  234. @end