Class: AmazingActivist::Polyglot

Inherits:
Object
  • Object
show all
Defined in:
lib/amazing_activist/polyglot.rb

Instance Method Summary collapse

Constructor Details

#initialize(activity) ⇒ Polyglot

Returns a new instance of Polyglot.



13
14
15
# File 'lib/amazing_activist/polyglot.rb', line 13

def initialize(activity)
  @activity = activity
end

Instance Method Details

#message(code, **context) ⇒ String

The i18n key for the message will be looked in namespaces:

Thus, if activity ‘Pretty::DamnGoodActivity` failed with `:bad_choise` code the lookup will be:

  • ‘amazing_activist.activities.pretty/damn_good_activity.failures.bad_choice

  • ‘activities.pretty/damn_good_activity.failures.bad_choice

  • ‘amazing_activist.failures.bad_choice

  • ‘activities.failures.bad_choice

If there’s no translation with any of the above keys, a generic non-translated message will be used:

<pretty/damn_good_activity> failed - bad_choice

Returns:

  • (String)

    Failure message



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/amazing_activist/polyglot.rb', line 38

def message(code, **context)
  activity = @activity.class.name&.underscore
  default  = [
    :"amazing_activist.failures.#{code}",
    :"activities.failures.#{code}",
    "<%{activity}> failed - %{code}" # rubocop:disable Style/FormatStringToken
  ]

  if activity
    i18n_key = :"amazing_activist.activities.#{activity}.failures.#{code}"
    default.unshift(:"activities.#{activity}.failures.#{code}")
  else
    activity = "(anonymous activity)"
    i18n_key = default.shift
  end

  I18n.t(i18n_key, **context, default: default, activity: activity, code: code)
end