Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Rails' i18n pluralization #1000

Closed
klappradla opened this issue Dec 20, 2017 · 2 comments
Closed

Support for Rails' i18n pluralization #1000

klappradla opened this issue Dec 20, 2017 · 2 comments

Comments

@klappradla
Copy link

klappradla commented Dec 20, 2017

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?

@justin808
Copy link
Member

@klappradla can you please do a PR for this? Great write up! 👏

I can help if needed. Please see the version history of the i18n files.

Please see #642 and #717

@JasonYCHuang Maybe you can comment?

@justin808
Copy link
Member

I added a "note" to https://github.com/shakacode/react_on_rails/blob/master/docs/basics/i18n.md.

PR's on this topic are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants