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.h | 94 +++++++++ iPhone/GTMUILocalizer.m | 207 +++++++++++++++++++ iPhone/GTMUILocalizerTest.h | 29 +++ iPhone/GTMUILocalizerTest.m | 87 ++++++++ iPhone/GTMUILocalizerTest.xib | 467 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 884 insertions(+) create mode 100644 iPhone/GTMUILocalizer.h create mode 100644 iPhone/GTMUILocalizer.m create mode 100644 iPhone/GTMUILocalizerTest.h create mode 100644 iPhone/GTMUILocalizerTest.m create mode 100644 iPhone/GTMUILocalizerTest.xib (limited to 'iPhone') diff --git a/iPhone/GTMUILocalizer.h b/iPhone/GTMUILocalizer.h new file mode 100644 index 0000000..2917e61 --- /dev/null +++ b/iPhone/GTMUILocalizer.h @@ -0,0 +1,94 @@ +// +// GTMUILocalizer.h +// +// 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 + +// A class for localizing nibs by doing simple string replacement. +// To use this, make an instance of GTMUILocalizer in your nib. Connect the +// owner_ outlet of the your instance to the File Owner of the nib. It expects +// the owner_ outlet to be an instance or subclass of NSWindowController, +// NSViewController or NSApplication. Using the bundle of the nib it will then +// localize any items in the NSWindowController's window and subviews, or the +// NSViewController's view and subviews or the NSApplication's main menu +// and dockmenu at when awakeFromNib is called on the GTMUILocalizer instance. +// You can optionally hook up otherObjectToLocalize_ and +// yetAnotherObjectToLocalize_ and those will also be localized. Strings in the +// nib that you want localized must start with ^ (shift-6). The strings will +// be looked up in the Localizable.strings table without the caret as the +// key. +// Things that will be localized are: +// - Titles and altTitles (for menus, buttons, windows, menuitems, tabViewItem) +// - stringValue (for labels) +// - tooltips +// - accessibility help +// - menus +// +// Due to technical limitations, accessibility description cannot be localized. +// See http://lists.apple.com/archives/Accessibility-dev/2009/Dec/msg00004.html +// and http://openradar.appspot.com/7496255 for more information. +// +// As an example if I wanted to localize a button with the word "Print" on +// it, I would put it in a window controlled by a NSWindowController that was +// the owner of the nib. I would set it's title to be "^Print". I would then +// create an instance of GTMUILocalizer and set it's owner_ to be the owner +// of the nib. +// In my Localizable.strings file in my fr.lproj directory for the bundle +// I would put "Print" = "Imprimer"; +// Then when my app launched in French I would get a button labeled +// "Imprimer". Note that GTMUILocalizer is only for strings, and doesn't +// resize, move or change text alignment on any of the things it modifies. +// If you absolutely need a caret at the beginning of the string +// post-localization, you can put "Foo" = "^Foo"; in your strings file and +// it will work. +// Your nib could be located in a variety of places depending on what you want +// to do. I would recommend having your "master" nib directly in Resources. +// If for some reason you needed to do some custom localization of the +// nib you could copy the master nib into your specific locale folder, and +// then you only need to adjust the items in the nib that you need to +// customize. You can leave the strings in the "^Foo" convention and they +// will localize properly. This keeps the differences between the nibs down +// to the bare essentials. +// +// NOTE: NSToolbar localization support is limited to only working on the +// default items in the toolbar. We cannot localize items that are on of the +// customization palette but not in the default items because there is not an +// API for NSToolbar to get all possible items. You are responsible for +// localizing all non-default toolbar items by hand. +// +@interface GTMUILocalizer : NSObject { + @private + IBOutlet id owner_; + IBOutlet id otherObjectToLocalize_; + IBOutlet id yetAnotherObjectToLocalize_; + NSBundle *bundle_; +} +- (id)initWithBundle:(NSBundle *)bundle; + +// Localize |object|. If |recursive| is true, it will attempt +// to localize objects owned/referenced by |object|. +- (void)localizeObject:(id)object recursively:(BOOL)recursive; + +// A method for subclasses to override in case you have a different +// way to go about getting localized strings. +// If |string| does not start with ^ you should return nil. +// If |string| is nil, you should return nil +- (NSString *)localizedStringForString:(NSString *)string; + +// Allows subclasses to override how the bundle is picked up ++ (NSBundle *)bundleForOwner:(id)owner; +@end 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 diff --git a/iPhone/GTMUILocalizerTest.h b/iPhone/GTMUILocalizerTest.h new file mode 100644 index 0000000..041444d --- /dev/null +++ b/iPhone/GTMUILocalizerTest.h @@ -0,0 +1,29 @@ +// +// GTMUILocalizerTest.h +// +// 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 + +#import "GTMUILocalizer.h" + +@interface GTMUILocalizerTestViewController : UIViewController { + @private + UILabel *label_; +} + +@property(nonatomic, retain)IBOutlet UILabel *label; + +@end diff --git a/iPhone/GTMUILocalizerTest.m b/iPhone/GTMUILocalizerTest.m new file mode 100644 index 0000000..116d170 --- /dev/null +++ b/iPhone/GTMUILocalizerTest.m @@ -0,0 +1,87 @@ +// +// GTMUILocalizerTest.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 "GTMUILocalizerTest.h" +#import "GTMSenTestCase.h" + +@interface TestUILocalizer : GTMUILocalizer +- (void)localize:(id)object; +@end + +@implementation TestUILocalizer +- (NSString *)localizedStringForString:(NSString *)string{ + return [string substringFromIndex:5]; +} + +- (void)localize:(id)object { + [self localizeObject:object recursively:YES]; +} +@end + + +@implementation GTMUILocalizerTestViewController + +@synthesize label = label_; + +- (id)init { + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + return [self initWithNibName:@"GTMUILocalizerTest" bundle:bundle]; +} +@end + +@interface GTMUILocalizerTest : GTMTestCase +@end + +@implementation GTMUILocalizerTest +- (void)testLocalization { + GTMUILocalizerTestViewController* controller = + [[GTMUILocalizerTestViewController alloc] init]; + + // Load the view. + [controller view]; + + STAssertEqualStrings(@"^IDS_FOO", [[controller label] text], nil); + +// Accessibility label seems to not be working at all. They always are nil. +// Even when setting those explicitely there, the getter always returns nil. +// This might cause because the gobal accessibility switch is not on during the +// tests. +#if 0 + STAssertEqualStrings(@"^IDS_FOO", [[controller view] accessibilityLabel], + nil); + STAssertEqualStrings(@"^IDS_FOO", [[controller view] accessibilityHint], + nil); + STAssertEqualStrings(@"^IDS_FOO", [[controller label] accessibilityLabel], + nil); + STAssertEqualStrings(@"^IDS_FOO", [[controller label] accessibilityHint], + nil); +#endif + + TestUILocalizer *localizer = [[TestUILocalizer alloc] init]; + [localizer localize:[controller view]]; + + STAssertEqualStrings(@"FOO", [[controller label] text], nil); + +// Accessibility label seems to not be working at all. They always are nil. +#if 0 + STAssertEqualStrings(@"FOO", [[controller view] accessibilityLabel], nil); + STAssertEqualStrings(@"FOO", [[controller view] accessibilityHint], nil); + STAssertEqualStrings(@"FOO", [[controller label] accessibilityLabel], nil); + STAssertEqualStrings(@"FOO", [[controller label] accessibilityHint], nil); +#endif +} +@end diff --git a/iPhone/GTMUILocalizerTest.xib b/iPhone/GTMUILocalizerTest.xib new file mode 100644 index 0000000..ed6a8a4 --- /dev/null +++ b/iPhone/GTMUILocalizerTest.xib @@ -0,0 +1,467 @@ + + + + 1056 + 10K549 + 851 + 1038.36 + 461.00 + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + 141 + + + YES + + + + YES + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + YES + + YES + + + YES + + + + YES + + IBFilesOwner + IBCocoaTouchFramework + + + IBFirstResponder + IBCocoaTouchFramework + + + + 292 + + YES + + + 292 + {{115, 182}, {136, 21}} + + NO + YES + 7 + NO + + ^IDS_FOO + ^IDS_FOO + + + IBCocoaTouchFramework + ^IDS_FOO + + 1 + MCAwIDAAA + + + 3 + MQA + + 1 + 10 + + + {320, 460} + + + 3 + MQA + + 2 + + + + ^IDS_FOO + ^IDS_FOO + + + IBCocoaTouchFramework + + + + + YES + + + view + + + + 7 + + + + label + + + + 13 + + + + + YES + + 0 + + + + + + -1 + + + File's Owner + + + -2 + + + + + 5 + + + YES + + + + + + 9 + + + + + + + YES + + YES + -1.CustomClassName + -2.CustomClassName + 5.IBEditorWindowLastContentRect + 5.IBPluginDependency + 5.IBViewBoundsToFrameTransform + 9.IBPluginDependency + + + YES + GTMUILocalizerTestViewController + UIResponder + {{870, 485}, {320, 460}} + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + P4AAAL+AAAAAAAAAw+UAAA + + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + + + YES + + + YES + + + + + YES + + + YES + + + + 15 + + + + YES + + GTMUILocalizerTestViewController + UIViewController + + label + UILabel + + + label + + label + UILabel + + + + IBProjectSource + iPhone/GTMUILocalizerTest.h + + + + NSObject + + IBProjectSource + Foundation/GTMNSObject+KeyValueObserving.h + + + + NSObject + + IBProjectSource + UnitTesting/GTMCALayer+UnitTesting.h + + + + NSObject + + gtm_unitTestEncoderWillEncode:inCoder: + id + + + gtm_unitTestEncoderWillEncode:inCoder: + + gtm_unitTestEncoderWillEncode:inCoder: + id + + + + IBProjectSource + UnitTesting/GTMNSObject+UnitTesting.h + + + + UIView + + IBProjectSource + UnitTesting/GTMUIKit+UnitTesting.h + + + + UIView + + IBProjectSource + iPhone/GTMUIView+SubtreeDescription.h + + + + + YES + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSError.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSFileManager.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSKeyValueCoding.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSKeyValueObserving.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSKeyedArchiver.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSObject.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSRunLoop.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSThread.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSURL.h + + + + NSObject + + IBFrameworkSource + Foundation.framework/Headers/NSURLConnection.h + + + + NSObject + + IBFrameworkSource + QuartzCore.framework/Headers/CAAnimation.h + + + + NSObject + + IBFrameworkSource + QuartzCore.framework/Headers/CALayer.h + + + + NSObject + + IBFrameworkSource + UIKit.framework/Headers/UIAccessibility.h + + + + NSObject + + IBFrameworkSource + UIKit.framework/Headers/UINibLoading.h + + + + NSObject + + IBFrameworkSource + UIKit.framework/Headers/UIResponder.h + + + + UILabel + UIView + + IBFrameworkSource + UIKit.framework/Headers/UILabel.h + + + + UIResponder + NSObject + + + + UISearchBar + UIView + + IBFrameworkSource + UIKit.framework/Headers/UISearchBar.h + + + + UISearchDisplayController + NSObject + + IBFrameworkSource + UIKit.framework/Headers/UISearchDisplayController.h + + + + UIView + + IBFrameworkSource + UIKit.framework/Headers/UIPrintFormatter.h + + + + UIView + + IBFrameworkSource + UIKit.framework/Headers/UITextField.h + + + + UIView + UIResponder + + IBFrameworkSource + UIKit.framework/Headers/UIView.h + + + + UIViewController + + IBFrameworkSource + UIKit.framework/Headers/UINavigationController.h + + + + UIViewController + + IBFrameworkSource + UIKit.framework/Headers/UIPopoverController.h + + + + UIViewController + + IBFrameworkSource + UIKit.framework/Headers/UISplitViewController.h + + + + UIViewController + + IBFrameworkSource + UIKit.framework/Headers/UITabBarController.h + + + + UIViewController + UIResponder + + IBFrameworkSource + UIKit.framework/Headers/UIViewController.h + + + + + 0 + IBCocoaTouchFramework + + com.apple.InterfaceBuilder.CocoaTouchPlugin.iPhoneOS + + + + com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3 + + + YES + ../GTMiPhone.xcodeproj + 3 + 141 + + -- cgit v1.2.3