Skip to content

Commit

Permalink
Removed plural extension from NSNumber in favor of https://github.com…
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Zats committed May 26, 2012
1 parent 7610eea commit f0e32b7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 237 deletions.
48 changes: 0 additions & 48 deletions SSToolkit/NSNumber+SSToolkitAdditions.h
Expand Up @@ -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:
<pre><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);
}</code></pre>
produces output:
<pre><code>0 tweets
1 tweet
2 tweet</code></pre>
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:
<pre><code>"pluralMinuteForms" = "minute,minutes";</code></pre>
Excerpt from `Localizable.strings` for Hebrew locale:
<pre><code>"pluralMinuteForms" = "דקה,דקותֿ";</code></pre>
Excerpt from `Localizable.strings` for Russian locale:
<pre><code>"pluralMinuteForms" = "минута,минуты,минут";</code></pre>
<pre><code>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);
}</code></pre>
@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
183 changes: 0 additions & 183 deletions SSToolkit/NSNumber+SSToolkitAdditions.m
Expand Up @@ -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];
Expand All @@ -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
12 changes: 6 additions & 6 deletions SSToolkit/UIControl+SSToolkitAdditions.m
Expand Up @@ -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];
}

Expand Down

0 comments on commit f0e32b7

Please sign in to comment.