From 5417511f06f82923c1f7d825b0c792fe4dfacada Mon Sep 17 00:00:00 2001 From: Max Melentiev Date: Mon, 15 Feb 2021 09:44:19 +0000 Subject: [PATCH] Fix translation helpers to support ruby 3 --- .../bot/updates_controller/translation.rb | 8 +-- .../updates_controller/translation_spec.rb | 57 +++++++++++++++++-- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lib/telegram/bot/updates_controller/translation.rb b/lib/telegram/bot/updates_controller/translation.rb index ee2ba12..179f76a 100644 --- a/lib/telegram/bot/updates_controller/translation.rb +++ b/lib/telegram/bot/updates_controller/translation.rb @@ -12,15 +12,15 @@ module Telegram module ClassMethods # Class-level helper for lazy translations. - def translate(key, options = {}) + def translate(key, **options) key = "#{controller_path.tr('/', '.')}#{key}" if key.to_s.start_with?('.') - I18n.translate(key, options) + I18n.translate(key, **options) end alias :t :translate end # See toplevel description. - def translate(key, options = {}) + def translate(key, **options) if key.to_s.start_with?('.') path = controller_path.tr('/', '.') defaults = [:"#{path}#{key}"] @@ -28,7 +28,7 @@ module Telegram options[:default] = defaults.flatten key = "#{path}.#{action_name_i18n_key}#{key}" end - I18n.translate(key, options) + I18n.translate(key, **options) end alias :t :translate diff --git a/spec/telegram/bot/updates_controller/translation_spec.rb b/spec/telegram/bot/updates_controller/translation_spec.rb index 534a20c..3368527 100644 --- a/spec/telegram/bot/updates_controller/translation_spec.rb +++ b/spec/telegram/bot/updates_controller/translation_spec.rb @@ -1,5 +1,26 @@ RSpec.describe Telegram::Bot::UpdatesController::Translation do + def self.stub_translations(translations) + around do |ex| + backend = I18n.backend + I18n.backend = I18n::Backend::Simple.new + begin + I18n.backend.store_translations 'en', telegram: {webhooks: translations} + ex.run + ensure + I18n.backend = backend + end + end + end + let(:result) { double(:result) } + describe '#translate' do + stub_translations( + start: { + hello: 'Hello from start!', + hi: 'Hi %{name} from start', # rubocop:disable Style/FormatStringToken + }, + ) + let(:controller) do double( controller_path: 'telegram/webhooks', @@ -10,12 +31,28 @@ RSpec.describe Telegram::Bot::UpdatesController::Translation do 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') + ) { result } + expect(controller.t('.hello')).to eq(result) + + expect(I18n).to receive(:translate).with('telegram.webhooks.start.hi', + default: [:'telegram.webhooks.hi'], + name: 'Dude', + ) { result } + expect(controller.t('.hi', name: 'Dude')).to eq(result) + end + + it 'translates' do + expect(controller.t('.hello')).to eq('Hello from start!') + expect(controller.t('.hi', name: 'Dude')).to eq('Hi Dude from start') end end describe described_class::ClassMethods do + stub_translations( + hello: 'Hello!', + hi: 'Hi %{name}', # rubocop:disable Style/FormatStringToken + ) + describe '#translate' do let(:controller_class) do double(controller_path: 'telegram/webhooks'). @@ -23,8 +60,20 @@ RSpec.describe Telegram::Bot::UpdatesController::Translation do end it 'uses controller_path for lazy translations' do - expect(I18n).to receive(:translate).with('telegram.webhooks.hello', {}) - controller_class.t('.hello') + expect(I18n).to receive(:translate) do |*args, **kwargs| + # not using .with to support ruby 2.x and 3.x + expect([*args, kwargs]).to eq(['telegram.webhooks.hello', {}]) + result + end + expect(controller_class.t('.hello')).to eq(result) + + expect(I18n).to receive(:translate).with('telegram.webhooks.hi', name: 'Dude') { result } + expect(controller_class.t('.hi', name: 'Dude')).to eq(result) + end + + it 'translates' do + expect(controller_class.t('.hello')).to eq('Hello!') + expect(controller_class.t('.hi', name: 'Dude')).to eq('Hi Dude') end end end