19

I18n.translate can translate error.messages like this:

I18n.translate('error.messages.taken')
-> has already been taken

But there are some error messages that contains arguments like:

I18n.translate('error.messages.greater_than_or_equal_to')
-> must be greater than or equal to %{count}"

Is it possible to pass the argument ‘count’ in the I18n.translate?

Leo
  • 583
  • 2
  • 5
  • 13

6 Answers6

29

You can pass the params after the key

I18n.translate('error.messages.greater_than_or_equal_to', count: 2)
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
6

For multiple params it could be:

I18n.translate('error.messages.greater_than_or_equal_to', {
  count: 2,
  foo: 'bar'
})
user3773080
  • 63
  • 1
  • 4
4

If you need to pass a hash(dynamic) argument, you can use the double splat operator **.

values = { count: 2, name: 'John' }
I18n.t('error.messages.greater_than_or_equal_to', **values)

Datt
  • 851
  • 9
  • 21
2

This would allow you to add as many arguments as you want

I18n.translate('error.messages.greater_than_or_equal_to {arg1}').replace('{arg1}', count)
Vijay
  • 1,082
  • 8
  • 6
0

If you have

"greeting": "hi {name}"

You need to write

i18n.translate('greeting', {values: {name: 'John'}});
nix86
  • 2,837
  • 11
  • 36
  • 69
0

In ROR I was able to pass multiple arguments using

#app/mailers/sample_mailer.rb

(to: email, subject: t('<mailer_scope>.<action_name>.subject', <locale_variable_name_1>: <Rails_variable_name_1>, <locale_variable_name_2>: <Rails_variable_name_2>))

#config/locales/en.yml

mailer_scope:
    action_name:
      `subject: "Insert e-mail subject here: %{locale_variable_name_1} - %{locale_variable_name_2}"

You may read this official docs from Rails guides.

Lea Tinoso
  • 1,663
  • 2
  • 6
  • 14