Method: ApplicationHelper#i18ns

Defined in:
app/helpers/application_helper.rb

#i18ns(key, **subs) ⇒ String

Translate a given string using I18n#t, after substituting values into the string based on a hash.

Examples:

# In I18n config:
# user_post_count: 'You have :count posts on this community.'
helpers.i18ns('user_post_count', count: @posts.size)
# => 'You have 23 posts on this community.'
# or as appropriate based on locale

Parameters:

  • key (String, Symbol)

    The translation key as passed to I18n#t.

  • subs (Hash{#to_s => #to_s})

    A list of substitutions to apply - keys of the form :name in the string should have a corresponding name entry in this hash and will be substituted for the value.

Returns:

  • (String)


259
260
261
262
263
264
265
# File 'app/helpers/application_helper.rb', line 259

def i18ns(key, **subs)
  s = I18n.t key
  subs.each do |f, r|
    s = s.gsub ":#{f}", r.to_s
  end
  s
end