1
0
зеркало из https://github.com/glebtv/telegram-bot.git synced 2026-09-08 03:45:52 +03:00

Translation helper strips ! from action name for lazy translations

Этот коммит содержится в:
Max Melentiev
2018-06-07 14:10:22 +06:00
родитель 6dd705fe89
Коммит 7ff89d012c
4 изменённых файлов: 66 добавлений и 8 удалений

Просмотреть файл

@@ -12,6 +12,7 @@
- `:telegram_bot` rspec tag is replaced with `telegram_bot: :rails`. - `:telegram_bot` rspec tag is replaced with `telegram_bot: :rails`.
- __Breaking change__. Use bang-methods as actions for commands. - __Breaking change__. Use bang-methods as actions for commands.
This prevents calling context contextual actions and payload specific actions with commands. This prevents calling context contextual actions and payload specific actions with commands.
Translation helper strips `!` from action name for lazy translations.
- __Breaking change__. Drop `.context_handler`, `.context_to_action!` methods. - __Breaking change__. Drop `.context_handler`, `.context_to_action!` methods.
Use pass action name directly to `#save_context`. Use pass action name directly to `#save_context`.
It's the same as `.context_to_action!` is enabled by default. It's the same as `.context_to_action!` is enabled by default.

Просмотреть файл

@@ -1,4 +1,5 @@
require 'abstract_controller' require 'abstract_controller'
require 'active_support/core_ext/string/inflections'
require 'active_support/callbacks' require 'active_support/callbacks'
require 'active_support/version' require 'active_support/version'
@@ -54,13 +55,14 @@ module Telegram
abstract! abstract!
%w[ %w[
commands Commands
instrumentation Instrumentation
log_subscriber LogSubscriber
reply_helpers ReplyHelpers
rescue Rescue
session Session
].each { |file| require "telegram/bot/updates_controller/#{file}" } Translation
].each { |name| require "telegram/bot/updates_controller/#{name.underscore}" }
%w[ %w[
CallbackQueryContext CallbackQueryContext
@@ -79,10 +81,12 @@ module Telegram
skip_after_callbacks_if_terminated: true skip_after_callbacks_if_terminated: true
end end
include AbstractController::Translation
include Commands include Commands
include Rescue include Rescue
include ReplyHelpers include ReplyHelpers
include Translation
# Add instrumentations hooks at the bottom, to ensure they instrument
# all the methods properly.
include Instrumentation include Instrumentation
extend Session::ConfigMethods extend Session::ConfigMethods

Просмотреть файл

@@ -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

Просмотреть файл

@@ -0,0 +1,17 @@
RSpec.describe Telegram::Bot::UpdatesController::Translation do
describe '#translate' do
let(:controller) do
double(
controller_path: 'telegram/webhooks',
action_name: 'start!',
).tap { |x| x.extend(described_class) }
end
it 'uses action_name without ! for lazy translations' do
expect(I18n).to receive(:translate).with('telegram.webhooks.start.hello',
default: [:'telegram.webhooks.hello'],
)
controller.t('.hello')
end
end
end