зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-04 18:25:51 +03:00
Translation helper strips ! from action name for lazy translations
Этот коммит содержится в:
@@ -1,4 +1,5 @@
|
||||
require 'abstract_controller'
|
||||
require 'active_support/core_ext/string/inflections'
|
||||
require 'active_support/callbacks'
|
||||
require 'active_support/version'
|
||||
|
||||
@@ -54,13 +55,14 @@ module Telegram
|
||||
abstract!
|
||||
|
||||
%w[
|
||||
commands
|
||||
instrumentation
|
||||
log_subscriber
|
||||
reply_helpers
|
||||
rescue
|
||||
session
|
||||
].each { |file| require "telegram/bot/updates_controller/#{file}" }
|
||||
Commands
|
||||
Instrumentation
|
||||
LogSubscriber
|
||||
ReplyHelpers
|
||||
Rescue
|
||||
Session
|
||||
Translation
|
||||
].each { |name| require "telegram/bot/updates_controller/#{name.underscore}" }
|
||||
|
||||
%w[
|
||||
CallbackQueryContext
|
||||
@@ -79,10 +81,12 @@ module Telegram
|
||||
skip_after_callbacks_if_terminated: true
|
||||
end
|
||||
|
||||
include AbstractController::Translation
|
||||
include Commands
|
||||
include Rescue
|
||||
include ReplyHelpers
|
||||
include Translation
|
||||
# Add instrumentations hooks at the bottom, to ensure they instrument
|
||||
# all the methods properly.
|
||||
include Instrumentation
|
||||
|
||||
extend Session::ConfigMethods
|
||||
|
||||
36
lib/telegram/bot/updates_controller/translation.rb
Обычный файл
36
lib/telegram/bot/updates_controller/translation.rb
Обычный файл
@@ -0,0 +1,36 @@
|
||||
module Telegram
|
||||
module Bot
|
||||
class UpdatesController
|
||||
# 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`.
|
||||
module Translation
|
||||
# See toplevel description.
|
||||
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
|
||||
alias :t :translate
|
||||
|
||||
# Strips trailing `!` from action_name.
|
||||
def action_name_i18n_key
|
||||
action_name.chomp('!')
|
||||
end
|
||||
|
||||
def localize(*args)
|
||||
I18n.localize(*args)
|
||||
end
|
||||
alias :l :localize
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Ссылка в новой задаче
Block a user