GithubHelp home page GithubHelp logo

alexguoq / tttlocalizedpluralstring Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mattt/tttlocalizedpluralstring

0.0 2.0 0.0 333 KB

NSLocalizedString with a Count Argument

License: MIT License

Objective-C 97.23% Ruby 2.77%

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)
  • 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)
  • Spanish (es)
  • Slovak (sk)
  • Swedish (sv)
  • Thai (th)
  • Turkish (tr)
  • Ukrainian (uk)
  • Vietnamese (vi)

Contact

Mattt Thompson

License

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

tttlocalizedpluralstring's People

Contributors

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

Watchers

gryphon avatar  avatar

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.