Module: Telegram::Bot::UpdatesController::Translation

Extended by:
ActiveSupport::Concern
Included in:
Telegram::Bot::UpdatesController
Defined in:
lib/telegram/bot/updates_controller/translation.rb

Overview

Provides helpers similar to AbstractController::Translation but by default uses ‘action_name_i18n_key` in lazy translation keys which strips `!` from action names by default. This makes translating strings for commands more convenient.

To disable this behaviour use ‘alias_method :action_name_i18n_key, :action_name`.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#action_name_i18n_keyObject

Strips trailing ‘!` from action_name.



38
39
40
# File 'lib/telegram/bot/updates_controller/translation.rb', line 38

def action_name_i18n_key
  action_name.chomp('!')
end

#localize(*args) ⇒ Object Also known as: l



42
43
44
# File 'lib/telegram/bot/updates_controller/translation.rb', line 42

def localize(*args)
  I18n.localize(*args)
end

#translate(key, **options) ⇒ Object Also known as: t

See toplevel description.



25
26
27
28
29
30
31
32
33
34
# File 'lib/telegram/bot/updates_controller/translation.rb', line 25

def translate(key, **options)
  if key.to_s.start_with?('.')
    path = controller_path.tr('/', '.')
    defaults = [:"#{path}#{key}"]
    defaults << options[:default] if options[:default]
    options[:default] = defaults.flatten
    key = "#{path}.#{action_name_i18n_key}#{key}"
  end
  I18n.translate(key, **options)
end