зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 09:46:17 +03:00
Class-level helper for lazy translations
Этот коммит содержится в:
@@ -16,6 +16,7 @@
|
||||
- __Breaking change__. Drop `.context_handler`, `.context_to_action!` methods.
|
||||
Use pass action name directly to `#save_context`.
|
||||
It's the same as `.context_to_action!` is enabled by default.
|
||||
- Class-level helper for lazy translations.
|
||||
|
||||
# 0.13.1
|
||||
|
||||
|
||||
@@ -8,6 +8,17 @@ module Telegram
|
||||
#
|
||||
# To disable this behaviour use `alias_method :action_name_i18n_key, :action_name`.
|
||||
module Translation
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
# Class-level helper for lazy translations.
|
||||
def translate(key, options = {})
|
||||
key = "#{controller_path.tr('/', '.')}#{key}" if key.to_s.start_with?('.')
|
||||
I18n.translate(key, options)
|
||||
end
|
||||
alias :t :translate
|
||||
end
|
||||
|
||||
# See toplevel description.
|
||||
def translate(key, options = {})
|
||||
if key.to_s.start_with?('.')
|
||||
|
||||
@@ -14,4 +14,18 @@ RSpec.describe Telegram::Bot::UpdatesController::Translation do
|
||||
controller.t('.hello')
|
||||
end
|
||||
end
|
||||
|
||||
describe described_class::ClassMethods do
|
||||
describe '#translate' do
|
||||
let(:controller_class) do
|
||||
double(controller_path: 'telegram/webhooks').
|
||||
tap { |x| x.extend(described_class) }
|
||||
end
|
||||
|
||||
it 'uses controller_path for lazy translations' do
|
||||
expect(I18n).to receive(:translate).with('telegram.webhooks.hello', {})
|
||||
controller_class.t('.hello')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Ссылка в новой задаче
Block a user