GithubHelp home page GithubHelp logo

tttlocalizedpluralstring's Introduction

TTTLocalizedPluralString

NSLocalizedString with a Count Argument

As of iOS 7 and Mac OS X 10.9 Mavericks, Foundation has the ability to specify localized strings according to pluralization and grammar rules. You can find more information about it in the Localized Property List File section of the Foundation release notes.


NSLocalizedString and its related macros 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:

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:

  • 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.

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: one) and %d Person (plural rule: other). Other localizations would only require the keys used by that language (e.g. 1 for Japanese, and 6 for Arabic).

Here is the full list of plural rules:

  • zero
  • one
  • two
  • few
  • many
  • other

Supported Locales

  • Arabic (ar)
  • Bulgarian (bg)
  • Catalan (ca)
  • Chinese (Simplified) (zh-Hans)
  • Chinese (Traditional) (zh-Hant)
  • Croatian (cr)
  • Czech (cs)
  • Danish (da)
  • Dutch (nl)
  • English (en)
  • German (de)
  • Finish (fi)
  • Greek (el)
  • French (fr)
  • Hebrew (he)
  • Hungarian (hu)
  • Indonesian (id)
  • Italian (it)
  • Japanese (ja)
  • Korean (ko)
  • Latvian (lv)
  • Malay (ms)
  • Norwegian Bokmål (nb)
  • Norwegian Nynorsk (nn)
  • Polish (pl)
  • Portuguese (pt)
  • Romanian (ro)
  • Russian (ru)
  • Serbian (sr)
  • Spanish (es)
  • Slovak (sk)
  • Slovenian (sl)
  • Swedish (sv)
  • Thai (th)
  • Turkish (tr)
  • Ukrainian (uk)
  • Vietnamese (vi)

Contact

Mattt

License

TTTLocalizedPluralString is available under the MIT license. See the LICENSE file for more info.

tttlocalizedpluralstring's People

Contributors

banshchikov avatar dklionsk avatar glwithu06 avatar jparise avatar kolyvan avatar mattt avatar michalkrause avatar nicksnyder avatar paulsamuels avatar rudensm avatar simondec avatar tantalic avatar yas375 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tttlocalizedpluralstring's Issues

key trouble

hi,

why does

TTTLocalizedPluralStringKeyForCountAndSingularNounForLanguage

return a string with format

@"%%d %@ (plural rule: %@)" 

this adds a %d to every key and will then not be found

e.g. i have a key-value pair in my Localizable.strings:

"tour.stop (plural rule: one)" = "%i Stop";
"tour.stop (plural rule: other)" = "%i Stops";

using "tour.stop" as the key with count=1 will create this new key: "%d tour.stop (plural rule: one)".
this key does not exist, so it will fail.

Is it really necessary to hardcode ALL the languages?

Thanks for making this @mattt!

I was wondering, is it really necessary to hardcode all the languages?

In most cases it would be enough to look for plural keys in order (0: zero, other; 1: one, few, many, other; 2: two, few, many, other; else: few, many, other). It appears only Arabic, Czech and Russian are more complex than this.

In pseudo-code:

if (language == russian) {
    russianLogic();
} else if (language == arabic) {
    arabicLogic();
} else if (language == Czech) {
    czechLogic();
} else {
    defaultLogic(); // Look for plural keys in order
}

Then you would avoid having to manually add each language.

Support strings like "I have %d cats"

I want to use your library but it seems to only support strings that begin with the number (e.g. "%d cats"). It would be nice to support strings like "I have %d cats".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.