Class: AmazingActivist::Polyglot
- Inherits:
-
Object
- Object
- AmazingActivist::Polyglot
- Defined in:
- lib/amazing_activist/polyglot.rb
Instance Method Summary collapse
-
#initialize(activity) ⇒ Polyglot
constructor
A new instance of Polyglot.
-
#message(code, **context) ⇒ String
The i18n key for the message will be looked in namespaces:.
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:
-
‘amazing_activist.activities..failures`
-
‘activities..failures`
-
‘amazing_activist.failures`
-
‘activities.failures`
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
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 (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 |