twitter/twitter-cldr-rb — pull request #22 by KL-7 (2012-04-26 23:02:35+00:00)
Title: Inline pluralization.

I added basic support for inline pluralization. The syntax is the following:

```ruby
f.format('there %<{ "yaks_count": {"one": "is 1 yak", "other": "are %{yaks_count} yaks"} }>', :yaks_count => 12)
#=> "there are 12 yaks"
```

There're two limitations:

* No space in openning `<{` and closing `}>` sequences. It can be changed, but it doesn't seem like a big limitation so I'd rather leave it that way.
* Only double quotes can be used to surround keys and strings inside pluralization group (in the example above you can't use `'one'` instead of `"one"`). That's a limitation of both standard json parser and [yajl-ruby](https://github.com/brianmario/yajl-ruby) gem. (Maybe it's a limitation of JSON itself, I need to check that).

I decided to throw an exception in two cases:

* When inline pluralization group is a hash of more than one element. Like

%<{ "yaks_count": {"one": "is 1 yak"}, "another_key": {"other": "are %{yaks_count} yaks"} }>'

that simply doesn't make sense. As I'm explicitly processing only the first key-value pair I think it's better to notify the user about the illegal format.
* When patterns are specified as smth other than a hash. Like

%<{ "yaks_count": 42 }>'

Again, the only reason for that to happen is some ridiculous mistake and throwing an exception will make it crystal clear what's the problem.

Exception will be raised in one more case: when our pluralization group (the string inside `%<...>`) is not a valid JSON. When that happens JSON parses throws it's own error about unexpected token or smth of that kind.

That's an initial implementation and we can discuss it if something need to be changed. Meanwhile, I'm going to add:

* more specs for `PluralFormatter` (missing patterns, numbers or whatever for inline pluralization, mixed pluralization styles in the same string, etc.);

* a couple of specs for inline pluralization on `LocalizedString#%` level just to make sure that everything plays nice together;

* some information and examples of inline interpolation to the documentation of `PluralFormatter#format`; while I agree that it's nice when the code is self-documented, I think some general information about such a versatile public method might be very useful;

One more thing: main part of the implementation is in [this](https://github.com/KL-7/twitter-cldr-rb/commit/58f1f256fb30972e7ba7d1ac7b78ec1da2aaaad2) commit, but I added a couple of others (some preparation stuff and minor clean-ups) into the same pull request. If you don't mind I'll do the same thing in the future (bundling some minor changes with the main feature in a single PR), because it's much easier than opening multiple pull requests (that will be dependent on each other) and we will end up with a cleaner commits history (less merge commits).