From f0e32b76a603d2df4a7e12c9d83db2228ccd52e4 Mon Sep 17 00:00:00 2001 From: Alexander Zats Date: Sat, 26 May 2012 21:53:23 +0300 Subject: [PATCH] Removed plural extension from NSNumber in favor of https://github.com/mattt/TTTLocalizedPluralString --- SSToolkit/NSNumber+SSToolkitAdditions.h | 48 ------ SSToolkit/NSNumber+SSToolkitAdditions.m | 183 ----------------------- SSToolkit/UIControl+SSToolkitAdditions.m | 12 +- 3 files changed, 6 insertions(+), 237 deletions(-) diff --git a/SSToolkit/NSNumber+SSToolkitAdditions.h b/SSToolkit/NSNumber+SSToolkitAdditions.h index ec6b185..82a4e7e 100644 --- a/SSToolkit/NSNumber+SSToolkitAdditions.h +++ b/SSToolkit/NSNumber+SSToolkitAdditions.h @@ -20,52 +20,4 @@ */ - (NSDate *)dateValue; -///-------------- -/// @name Localized plurals -///-------------- - -- (NSString *)pluralWithForms:(NSString *)pluralForms; - -/** - With given list of plural forms returns a correct one to use with specified number. So far supports only integers. - Following code: - -
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_us"];
- NSNumber *tweetsCount;
- for (int i = 0; i < 3; ++i) {
- tweetsCount = [NSNumber numberWithInteger:i];
- NSString *pluralForm = [tweetsCount pluralFormWithStringForms:@"tweet,tweets" locale:enUSLocale];
- NSLog(@"%@ %@", tweetsCount, pluralForm);
- }
- - produces output: - -
0 tweets
- 1 tweet
- 2 tweet
- - To create a truly localized experience, use `pluralFormWithStringForms:` method in conjunction with `Localizable.strings` files. It will automatically choose current system language: - - Excerpt from `Localizable.strings` for English locale: -
"pluralMinuteForms" = "minute,minutes";
- - Excerpt from `Localizable.strings` for Hebrew locale: -
"pluralMinuteForms" = "דקה,דקותֿ";
- - Excerpt from `Localizable.strings` for Russian locale: -
"pluralMinuteForms" = "минута,минуты,минут";
- -
for (int i = 0; i < 60; ++i) {
-	 NSNumber *minutes = [NSNumber numberWithInteger:i];
-	 NSString *pluralForm = [minutes pluralFormWithStringForms:NSLocalizedString(@"pluralMinuteForms", @"Correct form of the world `minute` will be chosen automatically")];
-	 NSLog(@"%@ %@", minutes, pluralForm);
- }
- - @param pluralForms comma-separated list of plural forms, e.g. `@"minute,minutes"` - @param language Canonicalized IETF BCP 47 language identifier, e.g. `en` for English (not `en_us`). - - @return One of passed stringForms that correspond to the integer representation of the number. - */ -- (NSString *)pluralWithForms:(NSString *)pluralForms language:(NSString *)language; - @end diff --git a/SSToolkit/NSNumber+SSToolkitAdditions.m b/SSToolkit/NSNumber+SSToolkitAdditions.m index c4b2e00..fd64865 100644 --- a/SSToolkit/NSNumber+SSToolkitAdditions.m +++ b/SSToolkit/NSNumber+SSToolkitAdditions.m @@ -8,21 +8,8 @@ #import "NSNumber+SSToolkitAdditions.h" -#define kDefaultLocaleIdetifier @"en" - -@interface NSNumber (SSToolkitAdditionsPrivate) - -// Returns correct form of the plural for spcified integer -typedef NSUInteger(^PluralFormSelector)(NSInteger integer); - -- (void)initializePluralForms; - -@end - @implementation NSNumber (SSToolkitAdditions) -static NSMutableDictionary *PluralFormSolvers; - - (NSDate *)dateValue { NSTimeInterval timestamp = [self doubleValue]; @@ -32,174 +19,4 @@ - (NSDate *)dateValue return [NSDate dateWithTimeIntervalSince1970:timestamp]; } -- (NSString *)pluralWithForms:(NSString *)pluralForms -{ - return [NSLocale preferredLanguages].count > 0 ? [self pluralWithForms:pluralForms language:[[NSLocale preferredLanguages] objectAtIndex:0]] : nil; -} - -- (NSString *)pluralWithForms:(NSString *)pluralForms language:(NSString *)language -{ - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [self initializePluralForms]; - }); - - PluralFormSelector pluralSolver = [PluralFormSolvers valueForKey:language]; - if (!pluralSolver) { - pluralSolver = [PluralFormSolvers valueForKey:kDefaultLocaleIdetifier]; - if (!pluralSolver) { - return nil; - } - } - - NSUInteger index = pluralSolver( [self integerValue]); - NSArray *components = [pluralForms componentsSeparatedByString:@","]; - if (components.count <= index) { - return nil; - } - return [components objectAtIndex:index]; - -} - @end - -@implementation NSNumber (SSToolkitAdditionsPrivate) - -- (void)initializePluralForms -{ - PluralFormSolvers = [NSMutableDictionary dictionary]; - - PluralFormSelector pluralSolver; - - // rule 0 (1 form) Asian (Chinese, Japanese, Korean, Vietnamese), Persian, Turkic/Altaic (Turkish), Thai, Lao - pluralSolver = [^NSUInteger(NSInteger n){ - return 0; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"zh"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ja"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ko"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"vi"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"fa"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"tr"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"th"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"lo"]; - - // rule #1 (2 forms) Germanic (Danish, Dutch, English, Faroese, Frisian, German, Norwegian, Swedish), Finno-Ugric - // (Estonian, Finnish, Hungarian), Language isolate (Basque), Latin/Greek (Greek), Semitic (Hebrew), Romanic - // (Italian, Portuguese, Spanish, Catalan) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n != 1) ? 1 : 0; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"da"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"nl"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"en"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"fo"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"fy"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"de"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"no"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"sv"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"sv"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"et"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"fi"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"hu"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"eu"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"el"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"he"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"it"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"pt"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"es"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ca"]; - - // rule #2 (2 forms) Romanic (French, Brazilian Portuguese) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n > 1) ? 1 : 0; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"fr"]; - // [PluralSolversDictionary setObject:pluralSolver forKey:@"??"]; - - // rule #3 (3 forms) Baltic (Latvian) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n % 10 == 1) && (n % 100 != 11) ? 1 : (n != 0) ? 2 : 0; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"lv"]; - - // rule #4 (4 forms) Celtic (Scottish Gaelic) - pluralSolver = [^NSUInteger(NSInteger n){ - return ((n == 1) || (n == 11)) ? 0 : ((n == 2) || (n == 12)) ? 1 : ((n > 0) && (n < 20)) ? 2 : 3; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"gd"]; - - // rule #5 (3 forms) Romanic (Romanian) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n == 1) ? 0 : ((n == 0) || ((n % 100 > 0) && (n % 100 < 20))) ? 1 : 2; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ro"]; - - // rule #6 (3 forms) Baltic (Lithuanian) - pluralSolver = [^NSUInteger(NSInteger n){ - return ((n % 10 == 1) && (n % 100 != 11)) ? 0 : (((n % 10 >= 2) && (n % 100 < 10)) || (n % 100 >= 20)) ? 2 : 1; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"lt"]; - - // rule #7 (3 forms) Slavic (Bosnian, Croatian, Serbian, Russian, Ukrainian) - pluralSolver = [^NSUInteger(NSInteger n){ - return ((n % 10 == 1) && (n % 100 != 11)) ? 0 : ((n % 10 >= 2) && (n % 10 <= 4) && ((n % 100 < 10) || n % 100 >= 20)) ? 1 : 2; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"bs"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"hr"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"sr"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ru"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"uk"]; - - // rule #8 (3 forms) Slavic (Slovak, Czech) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n == 1) ? 0 : ((n >= 2) && (n <= 4)) ? 1 : 2; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"sk"]; - [PluralFormSolvers setObject:pluralSolver forKey:@"cs"]; - - // rule #9 (3 forms) Slavic (Polish) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n == 1) ? 0 : ((n % 10 >= 2) && (n % 10 <= 4) && ((n % 100 < 10) || (n % 100 >= 20))) ? 1 : 2; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"pl"]; - - // rule #10 (4 forms) Slavic (Slovenian, Sorbian) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n % 100 == 1) ? 0 : (n % 100 == 2) ? 1 : ((n % 100 == 3) || (n % 100 == 4)) ? 2 : 3; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"sl"]; - // [PluralSolversDictionary setObject:pluralSolver forKey:@"??"]; - - // rule #11 (5 forms) Celtic (Irish Gaelic) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n == 1) ? 0 : (n == 2) ? 1 : (n >= 3) && (n <= 6) ? 2 : ((n >= 7) && (n <= 10)) ? 3 : 4; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ga"]; - - // rule #12 (6 forms) Semitic (Arabic) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n == 0) ? 5 : (n == 1) ? 0 : (n == 2) ? 1 : ((n % 100 >= 3) && (n % 100 <= 10)) ? 2 : ((n % 100 >= 11) && (n % 100 <= 99)) ? 3 : 4; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"ar"]; - - // rule #13 (4 forms) Semitic (Maltese) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n == 1) ? 0 : ((n == 0) || ((n % 100 > 0) && (n % 100 <= 10))) ? 1 : ((n % 100 > 10) && (n % 100 < 20)) ? 2 : 3; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"mt"]; - - // rule #14 (3 forms) Slavic (Macedonian) - pluralSolver = [^NSUInteger(NSInteger n){ - return (n % 10 == 1) ? 0 : (n % 10 == 2) ? 1 : 2; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"mk"]; - - // rule #15 (2 forms) Icelandic - pluralSolver = [^NSUInteger(NSInteger n){ - return ((n % 10 == 1) && (n % 100 != 11)) ? 0 : 1; - } copy]; - [PluralFormSolvers setObject:pluralSolver forKey:@"is"]; -} - -@end \ No newline at end of file diff --git a/SSToolkit/UIControl+SSToolkitAdditions.m b/SSToolkit/UIControl+SSToolkitAdditions.m index 9d48314..1367dee 100644 --- a/SSToolkit/UIControl+SSToolkitAdditions.m +++ b/SSToolkit/UIControl+SSToolkitAdditions.m @@ -19,12 +19,12 @@ - (void)removeAllTargets { - (void)setTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents { NSSet *targets = [self allTargets]; - [targets enumerateObjectsUsingBlock:^(id target, BOOL *stop) { - NSArray *actions = [self actionsForTarget:target forControlEvent:controlEvents]; - [actions enumerateObjectsUsingBlock:^(NSString *action, NSUInteger idx, BOOL *stop) { - [self removeTarget:target action:NSSelectorFromString(action) forControlEvents:controlEvents]; - }]; - }]; + for (id currentTarget in targets) { + NSArray *actions = [self actionsForTarget:currentTarget forControlEvent:controlEvents]; + for (NSString *currentAction in actions) { + [self removeTarget:currentTarget action:NSSelectorFromString(currentAction) forControlEvents:controlEvents]; + } + } [self addTarget:target action:action forControlEvents:controlEvents]; }