From 711247f40bf28cc93e8be3e74bc5d654884c9ce4 Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Wed, 16 Nov 2011 14:30:21 +0000 Subject: [Author: qsr] Handle UIButton in GTMUILocalizer To be noted: UISegmented Controls and UISearchBars were alredy working. UISwitch doesn't have any strings to be localized. R=dmaclach,thomasvl APPROVED=thomasvl --- iPhone/GTMUILocalizer.h | 5 - iPhone/GTMUILocalizer.m | 23 ++++- iPhone/GTMUILocalizerTest.h | 6 ++ iPhone/GTMUILocalizerTest.m | 72 ++++++++----- iPhone/GTMUILocalizerTest.xib | 229 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 296 insertions(+), 39 deletions(-) (limited to 'iPhone') diff --git a/iPhone/GTMUILocalizer.h b/iPhone/GTMUILocalizer.h index d34c4d0..7493c89 100644 --- a/iPhone/GTMUILocalizer.h +++ b/iPhone/GTMUILocalizer.h @@ -31,11 +31,6 @@ // be looked up in the Localizable.strings table without the caret as the // key. // -// TODO(tvl): this correct for iOS? -// 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 view controlled by a UIViewController that was // the owner of the nib. I would set it's title to be "^Print". I would then diff --git a/iPhone/GTMUILocalizer.m b/iPhone/GTMUILocalizer.m index efebc42..c6b7fca 100644 --- a/iPhone/GTMUILocalizer.m +++ b/iPhone/GTMUILocalizer.m @@ -26,7 +26,7 @@ // -[self localizeObject:recursively:]. - (void)localizeToolbar:(UIToolbar *)toolbar; - (void)localizeView:(UIView *)view recursively:(BOOL)recursive; - +- (void)localizeButton:(UIButton *)button; @end @implementation GTMUILocalizer @@ -130,6 +130,11 @@ } } + // 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:)]) { @@ -212,4 +217,20 @@ } } +- (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 diff --git a/iPhone/GTMUILocalizerTest.h b/iPhone/GTMUILocalizerTest.h index 87a0f22..0ee846c 100644 --- a/iPhone/GTMUILocalizerTest.h +++ b/iPhone/GTMUILocalizerTest.h @@ -22,8 +22,14 @@ @interface GTMUILocalizerTestViewController : UIViewController { @private UILabel *label_; + UIButton *button_; + UISegmentedControl *segmentedControl_; + UISearchBar *searchBar_; } @property(nonatomic, retain) IBOutlet UILabel *label; +@property(nonatomic, retain) IBOutlet UIButton *button; +@property(nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl; +@property(nonatomic, retain) IBOutlet UISearchBar *searchBar; @end diff --git a/iPhone/GTMUILocalizerTest.m b/iPhone/GTMUILocalizerTest.m index 682285d..dfe1f32 100644 --- a/iPhone/GTMUILocalizerTest.m +++ b/iPhone/GTMUILocalizerTest.m @@ -24,7 +24,10 @@ @implementation TestUILocalizer - (NSString *)localizedStringForString:(NSString *)string { - return [string substringFromIndex:5]; + if ([string length] >= 5) + return [string substringFromIndex:5]; + else + return string; } - (void)localize:(id)object { @@ -36,6 +39,9 @@ @implementation GTMUILocalizerTestViewController @synthesize label = label_; +@synthesize button = button_; +@synthesize segmentedControl = segmentedControl_; +@synthesize searchBar = searchBar_; - (id)init { NSBundle *bundle = [NSBundle bundleForClass:[self class]]; @@ -44,44 +50,64 @@ @end @interface GTMUILocalizerTest : GTMTestCase +- (void)checkValues:(NSString *)value + onController:(GTMUILocalizerTestViewController *)controller; @end @implementation GTMUILocalizerTest -- (void)testLocalization { - GTMUILocalizerTestViewController* controller = - [[GTMUILocalizerTestViewController alloc] init]; - - // Load the view. - [controller view]; - - STAssertEqualStrings(@"^IDS_FOO", [[controller label] text], nil); +- (void)checkValues:(NSString *)value + onController:(GTMUILocalizerTestViewController *)controller { + // Label + STAssertEqualStrings(value, [[controller label] text], nil); + // Button + UIControlState allStates[] = { UIControlStateNormal, + UIControlStateHighlighted, + UIControlStateDisabled, + UIControlStateSelected }; + for (size_t idx = 0; idx < (sizeof(allStates)/sizeof(allStates[0])) ; ++idx) { + UIControlState state = allStates[idx]; + STAssertEqualStrings(value, [[controller button] titleForState:state], nil); + } + // SegementedControl + for (NSUInteger i = 0; + i < [[controller segmentedControl] numberOfSegments]; + ++i) { + STAssertEqualStrings(value, + [[controller segmentedControl] titleForSegmentAtIndex:i], nil); + } + // SearchBar + STAssertEqualStrings(value, [[controller searchBar] text], nil); + STAssertEqualStrings(value, [[controller searchBar] placeholder], nil); + STAssertEqualStrings(value, [[controller searchBar] prompt], 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. +// Even when setting those explicitly 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], + STAssertEqualStrings(value, [[controller view] accessibilityLabel], nil); - STAssertEqualStrings(@"^IDS_FOO", [[controller view] accessibilityHint], + STAssertEqualStrings(value, [[controller view] accessibilityHint], nil); - STAssertEqualStrings(@"^IDS_FOO", [[controller label] accessibilityLabel], + STAssertEqualStrings(value, [[controller label] accessibilityLabel], nil); - STAssertEqualStrings(@"^IDS_FOO", [[controller label] accessibilityHint], + STAssertEqualStrings(value, [[controller label] accessibilityHint], nil); #endif +} + +- (void)testLocalization { + GTMUILocalizerTestViewController *controller = + [[GTMUILocalizerTestViewController alloc] init]; + + // Load the view. + [controller view]; + + [self checkValues:@"^IDS_FOO" onController:controller]; 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 + [self checkValues:@"FOO" onController:controller]; } @end diff --git a/iPhone/GTMUILocalizerTest.xib b/iPhone/GTMUILocalizerTest.xib index ed6a8a4..2f4379f 100644 --- a/iPhone/GTMUILocalizerTest.xib +++ b/iPhone/GTMUILocalizerTest.xib @@ -45,7 +45,7 @@ 292 - {{115, 182}, {136, 21}} + {{98, 132}, {136, 21}} NO YES @@ -62,13 +62,91 @@ 1 MCAwIDAAA - + 3 MQA 1 10 + + + 292 + {{81, 73}, {143, 37}} + + NO + IBCocoaTouchFramework + 0 + 0 + + Helvetica-Bold + 15 + 16 + + 1 + ^IDS_FOO + ^IDS_FOO + ^IDS_FOO + ^IDS_FOO + + + 1 + MC4xOTYwNzg0MzQ2IDAuMzA5ODAzOTMyOSAwLjUyMTU2ODY1NgA + + + 3 + MC41AA + + + + + 292 + {{44, 289}, {207, 44}} + + NO + IBCocoaTouchFramework + 2 + 0 + + YES + ^IDS_FOO + ^IDS_FOO + + + YES + + + + + YES + + + + + YES + {0, 0} + {0, 0} + + + YES + + + + + + + 290 + {{0, 199}, {320, 75}} + + 3 + IBCocoaTouchFramework + ^IDS_FOO + ^IDS_FOO + ^IDS_FOO + + IBCocoaTouchFramework + + {320, 460} @@ -106,6 +184,38 @@ 13 + + + delegate + + + + 26 + + + + button + + + + 28 + + + + segmentedControl + + + + 29 + + + + searchBar + + + + 30 + @@ -132,7 +242,10 @@ YES + + + @@ -141,6 +254,21 @@ + + 16 + + + + + 17 + + + + + 18 + + + @@ -149,21 +277,35 @@ YES -1.CustomClassName -2.CustomClassName + 16.IBPluginDependency + 17.IBPluginDependency + 18.IBPluginDependency + 18.IBViewBoundsToFrameTransform 5.IBEditorWindowLastContentRect 5.IBPluginDependency 5.IBViewBoundsToFrameTransform 9.IBPluginDependency + 9.IBViewBoundsToFrameTransform YES GTMUILocalizerTestViewController UIResponder - {{870, 485}, {320, 460}} + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + P4AAAL+AAABBUAAAw4cAAA + + {{299, 515}, {320, 460}} com.apple.InterfaceBuilder.IBCocoaTouchPlugin P4AAAL+AAAAAAAAAw+UAAA com.apple.InterfaceBuilder.IBCocoaTouchPlugin + + P4AAAL+AAABC5gAAw0kAAA + @@ -182,7 +324,7 @@ - 15 + 30 @@ -191,14 +333,49 @@ GTMUILocalizerTestViewController UIViewController - label - UILabel + YES + + YES + button + label + searchBar + segmentedControl + + + YES + UIButton + UILabel + UISearchBar + UISegmentedControl + - label - - label - UILabel + YES + + YES + button + label + searchBar + segmentedControl + + + YES + + button + UIButton + + + label + UILabel + + + searchBar + UISearchBar + + + segmentedControl + UISegmentedControl + @@ -206,6 +383,14 @@ iPhone/GTMUILocalizerTest.h + + GTMUILocalizerTestViewController + UIViewController + + IBUserSource + + + NSObject @@ -360,6 +545,22 @@ UIKit.framework/Headers/UIResponder.h + + UIButton + UIControl + + IBFrameworkSource + UIKit.framework/Headers/UIButton.h + + + + UIControl + UIView + + IBFrameworkSource + UIKit.framework/Headers/UIControl.h + + UILabel UIView @@ -389,6 +590,14 @@ UIKit.framework/Headers/UISearchDisplayController.h + + UISegmentedControl + UIControl + + IBFrameworkSource + UIKit.framework/Headers/UISegmentedControl.h + + UIView -- cgit v1.2.3