Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed Dec 6, 2011
0 parents commit fdf982a
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2011 Mattt Thompson (http://mattt.me/)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
@@ -0,0 +1,48 @@
# TTTLocalizedPluralString
## NSLocalizedString with a Count Argument

`NSLocalizedString` and its related macros are make localizing Mac and iOS applications relatively straight-forward and simple. It falls down, however, when having to deal with strings whose conjugations change based on a dynamic count value. In such cases, you may have seen code like this:

``` objective-c
if (count == 1) {
return NSLocalizedString(@"1 Person", nil);
} else {
return [NSString stringWithFormat:NSLocalizedString(@"%d People", nil), count];
}
```

While this works alright for English, you run into problems when targeting other locales. Consider some examples as described in the [Unicode Language Plural Rules](http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html):

* Many Asian languages, like Japanese, Korean, Thai, and Simplified Chinese, do not have plural forms of nouns
* Arabic has several plural forms, including rules for zero, one, two, as well as few, many, and other, which are determined using a rather complicated base 10 divmod operation to determine the parity.

Fortunately, `TTTLocalizedPluralString` figures all of this out for you. You can use it just as you would `NSLocalizedString`, only in this case, you also have an argument for count. Here is the example from before, this time using `TTTLocalizedPluralString`.

``` objective-c
return TTTLocalizedPluralString(count, @"Person", nil);
```

This macro points to a function that determines the plural rule for the current locale, and then does an `NSBundle` localized string lookup for the corresponding value. In this case, `en.lproj/Localizable.strings` would have two keys for this: `%d Person (plural rule: singular)` and `%d Person (plural rule: plural)`. Other localizations would only require the keys used by that language (e.g. 1 for Japanese, and 6 for Arabic).

## Next Steps

`TTTLocalizedPluralString` is safe to use in production applications. However, there are a few features that are currently missing that make this a really great solution. Consider this a wish list / roadmap for this project:

* Command-line tool to generate the keys necessary for your project / integration with `genstrings`
* Proper documentation
* Example project with sample usage
* Plural rules for all localizations supported on iOS

---

## Contact

Mattt Thompson

- http://github.com/mattt
- http://twitter.com/mattt
- m@mattt.me

## License

TTTLocalizedPluralString is available under the MIT license. See the LICENSE file for more info.
37 changes: 37 additions & 0 deletions TTTLocalizedPluralString.h
@@ -0,0 +1,37 @@
// TTTLocalizedPluralString.h
//
// Copyright (c) 2011 Mattt Thompson (http://mattt.me)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <Foundation/Foundation.h>

extern NSString * TTTLocalizedPluralStringKeyForCountAndSingularNoun(NSUInteger count, NSString *singular);

#define TTTLocalizedPluralString(count, singular, comment) \
[NSString stringWithFormat:[[NSBundle mainBundle] localizedStringForKey:TTTLocalizedPluralStringKeyForCountAndSingularNoun(count, singular) value:@"" table:nil], count]

#define TTTLocalizedPluralStringFromTable(count, singular, tbl, comment) \
[NSString stringWithFormat:[[NSBundle mainBundle] localizedStringForKey:TTTLocalizedPluralStringKeyForCountAndSingularNoun(count, singular) value:@"" table:(tbl)] count]

#define TTTLocalizedPluralStringFromTableInBundle(count, singular, tbl, bundle, comment) \
[NSString stringWithFormat:[bundle localizedStringForKey:TTTLocalizedPluralStringKeyForCountAndSingularNoun(count, singular) value:@"" table:(tbl)] count]

#define TTTLocalizedPluralStringWithDefaultValue(count, singular, tbl, bundle, val, comment) \
[NSString stringWithFormat:[bundle localizedStringForKey:TTTLocalizedPluralStringKeyForCountAndSingularNoun(count, singular) value:(val) table:(tbl)], count]
159 changes: 159 additions & 0 deletions TTTLocalizedPluralString.m
@@ -0,0 +1,159 @@
// TTTLocalizedPluralString.h
//
// Copyright (c) 2011 Mattt Thompson (http://mattt.me)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "TTTLocalizedPluralString.h"

// Source: Unicode Common Locale Data Repository Plural Rules
// http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html

static NSString * const kTTTZeroPluralRule = @"zero";
static NSString * const kTTTOnePluralRule = @"one";
static NSString * const kTTTTwoPluralRule = @"two";
static NSString * const kTTTFewPluralRule = @"few";
static NSString * const kTTTManyPluralRule = @"many";
static NSString * const kTTTOtherPluralRule = @"other";

static NSString * TTTArabicPluralRuleForCount(NSUInteger count) {
switch (count) {
case 0:
return kTTTZeroPluralRule;
case 1:
return kTTTOnePluralRule;
case 2:
return kTTTTwoPluralRule;
default: {
NSUInteger mod10 = count % 10;
if (mod10 >= 3 && mod10 <= 10) {
return kTTTFewPluralRule;
} else if (mod10 >= 11) {
return kTTTManyPluralRule;
} else {
return kTTTOtherPluralRule;
}
}
}
}

static NSString * TTTSimplifiedChinesePluralRuleForCount(NSUInteger count) {
return kTTTOtherPluralRule;
}


static NSString * TTTEnglishPluralRuleForCount(NSUInteger count) {
switch (count) {
case 1:
return kTTTOnePluralRule;
default:
return kTTTOtherPluralRule;
}
}

static NSString * TTTFrenchPluralRuleForCount(NSUInteger count) {
switch (count) {
case 1:
return kTTTOnePluralRule;
default:
return kTTTOtherPluralRule;
}
}

static NSString * TTTGermanPluralRuleForCount(NSUInteger count) {
switch (count) {
case 1:
return kTTTOnePluralRule;
default:
return kTTTOtherPluralRule;
}
}

static NSString * TTTItalianPluralRuleForCount(NSUInteger count) {
switch (count) {
case 1:
return kTTTOnePluralRule;
default:
return kTTTOtherPluralRule;
}
}

static NSString * TTTJapanesePluralRuleForCount(NSUInteger count) {
return kTTTOtherPluralRule;
}

static NSString * TTTKoreanPluralRuleForCount(NSUInteger count) {
return kTTTOtherPluralRule;
}

static NSString * TTTPortuguesePluralRuleForCount(NSUInteger count) {
switch (count) {
case 1:
return kTTTOnePluralRule;
default:
return kTTTOtherPluralRule;
}
}

static NSString * TTTSpanishPluralRuleForCount(NSUInteger count) {
switch (count) {
case 1:
return kTTTOnePluralRule;
default:
return kTTTOtherPluralRule;
}
}

static NSString * TTTThaiPluralRuleForCount(NSUInteger count) {
return kTTTOtherPluralRule;
}

NSString * TTTLocalizedPluralStringKeyForCountAndSingularNoun(NSUInteger count, NSString *singular) {
NSString *languageCode = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];

NSString *pluralRule = nil;
if ([languageCode isEqualToString:@"ar"]) {
pluralRule = TTTArabicPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"zh-hans"]) {
pluralRule = TTTSimplifiedChinesePluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"en"]) {
pluralRule = TTTEnglishPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"fr"]) {
pluralRule = TTTFrenchPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"de"]) {
pluralRule = TTTGermanPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"it"]) {
pluralRule = TTTItalianPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"ja"]) {
pluralRule = TTTJapanesePluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"ko"]) {
pluralRule = TTTKoreanPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"pt"]) {
pluralRule = TTTPortuguesePluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"es"]) {
pluralRule = TTTSpanishPluralRuleForCount(count);
} else if ([languageCode isEqualToString:@"th"]) {
pluralRule = TTTThaiPluralRuleForCount(count);
} else {
NSLog(@"Unsupported language: %@", languageCode);
return nil;
}

return [NSString stringWithFormat:@"%%d %@ (plural rule: %@)", singular, pluralRule];
}

0 comments on commit fdf982a

Please sign in to comment.