Clean template for localizing offers in Rails i18n

I have the following sentence that needs to be localized:

you have U unread message / s and N new message / s

Localization example

U 0 N 1

"you have 1 new message"

U 1 N 1

"you have 1 unread message and 1 new message"

U 1 N 0

"you have 1 unread message

U 2 N 0

"you have 1 unread message"

I can easily start with this mess

unread_only: one: you have 1 unread message other: you have {{count}} unread messages new_only: one: you have 1 new message other: you have {{count}} new messages 

... at this moment I am stuck

 # how do I pass two counts in? new_and_unread: 

I know that i18n in Rails is not an ICU MessageFormat, however, is there a reasonable way to localize this using the tools we have? Can you transfer 2 accounts to the localization switch?

+6
source share
1 answer

My first thought is to use something like:

 unread: one: 1 unread message other:{{count}} unread messages new: one: 1 new message other: {{count}} new messages I18n.t('you_have') << [msg1,msg2].map(&:presence).compact.join(I18n.t('and')) 

But that probably won't work for every language. Just western.

Here is additional information: http://guides.rubyonrails.org/i18n.html#passing-variables-to-translations

 new_and_unread: "You have %{new} new, and %{unread} unread messages" <%=t 'new_and_unread', :new => 1, :unread => 3 %> 
+5
source

Source: https://habr.com/ru/post/946134/


All Articles