From f62c657740d40e71c9fd6192e14d3b371970f4b3 Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Fri, 28 Oct 2011 15:00:21 +0000 Subject: [Author: qsr] Implements GTMUILocalizer for iOS. The interface is exactly the same, the implementation does walk the UIKit view hierarchy, instead of the Cocoa one. R=thomasvl APPROVED=thomasvl --- iPhone/GTMUILocalizer.m | 207 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 iPhone/GTMUILocalizer.m (limited to 'iPhone/GTMUILocalizer.m') diff --git a/iPhone/GTMUILocalizer.m b/iPhone/GTMUILocalizer.m new file mode 100644 index 0000000..5eff255 --- /dev/null +++ b/iPhone/GTMUILocalizer.m @@ -0,0 +1,207 @@ +// +// 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:] otherwise bindings will not be +// localized properly. +- (void)localizeToolbar:(UIToolbar *)toolbar; +- (void)localizeView:(UIView *)view recursively:(BOOL)recursive; + +@end + +@implementation GTMUILocalizer +- (id)initWithBundle:(NSBundle *)bundle { + if ((self = [super init])) { + bundle_ = [bundle retain]; + } + return self; +} + +- (void)dealloc { + [bundle_ release]; + [super dealloc]; +} + +- (void)awakeFromNib { + if (owner_) { + NSBundle *newBundle = [[self class] bundleForOwner:owner_]; + bundle_ = [newBundle retain]; + [self localizeObject:owner_ recursively:YES]; + [self localizeObject:otherObjectToLocalize_ recursively:YES]; + [self localizeObject:yetAnotherObjectToLocalize_ recursively:YES]; + } else { + _GTMDevLog(@"Expected an owner_ set for %@", self); + } +} + ++ (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:[UIView class]]) { + [self localizeView:(UIView *)object recursively:recursive]; + } else if ([object isKindOfClass:[UIToolbar class]]) { + [self localizeToolbar:(UIToolbar*)object]; + } + } +} + +- (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)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]; + } + } + + // 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]; + } + } + } +} + +@end -- cgit v1.2.3