Skip to content

Support for Rails' i18n pluralization #1000

@klappradla

Description

@klappradla

Hi 👋

first of all: thanx for your awesome work on this 🙏 - really helpful tool!

I came across a tiny "incompatibility" issue with Rails' conventions when using i18n with react-intl: the special role of the count variable working as both, a variable for interpolation and as a key to pick up the right pluralization is not taken into account by react_on_rails (see the "pluralization" section in the Rails i18n docs for details on this).

A short example
With an exemplary locales file like this:

en: inbox: one: 'one message', other: '{count} messages'

I can do this in Rails:

I18n.translate :inbox, count: 2 # => '2 messages' I18n.translate :inbox, count: 1 # => 'one message' I18n.translate :inbox, count: 10 # => '10 message' I18n.translate :inbox, count: 0 # => '0 messages'

Depending on the count variable, Rails automatically picks up the right pluralization plus the value of count can also be interpolated into the message.

This would end up in the generated defaults.js file in approximately this format:

"inboxOne": { "id":"inbox.one", "defaultMessage": "one message" }, "inboxOther": { "id":"inbox.other", "defaultMessage": "{count} messages" }

☝️ this means I have to explicitly append the one and other bits in order to match a valid translation ID, otherwise react-intl won't find it.
My current workaround for this would look like this:

<FormattedMessage id={`inbox.${inbox.length === 1 ? 'one' : 'other'}`} values={{ count: inbox.length }} />

This works but is a) a bit verbose and b) sadly not using the rich possibilities Format.js would offer for solving all these problems.

I would ❤️ react_on_rails to be able to stay consisted with the way Rails deals with pluralization here and e.g. transparently parse it accordingly under the hood.

Do you have any thoughts on this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions