зеркало из
https://github.com/glebtv/telegram-bot.git
synced 2026-09-03 17:55:52 +03:00
Сравнить коммиты
3 Коммитов
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
a7cf3754be | ||
|
|
783d9c13e9 | ||
|
|
5417511f06 |
@@ -1,5 +1,9 @@
|
|||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
|
# 0.15.3
|
||||||
|
|
||||||
|
- Ruby 3.0 support: fix translation helpers.
|
||||||
|
|
||||||
# 0.15.2
|
# 0.15.2
|
||||||
|
|
||||||
- Ruby 3.0 support. Drop support for Ruby < 2.4.
|
- Ruby 3.0 support. Drop support for Ruby < 2.4.
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ module Telegram
|
|||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Class-level helper for lazy translations.
|
# 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?('.')
|
key = "#{controller_path.tr('/', '.')}#{key}" if key.to_s.start_with?('.')
|
||||||
I18n.translate(key, options)
|
I18n.translate(key, **options)
|
||||||
end
|
end
|
||||||
alias :t :translate
|
alias :t :translate
|
||||||
end
|
end
|
||||||
|
|
||||||
# See toplevel description.
|
# See toplevel description.
|
||||||
def translate(key, options = {})
|
def translate(key, **options)
|
||||||
if key.to_s.start_with?('.')
|
if key.to_s.start_with?('.')
|
||||||
path = controller_path.tr('/', '.')
|
path = controller_path.tr('/', '.')
|
||||||
defaults = [:"#{path}#{key}"]
|
defaults = [:"#{path}#{key}"]
|
||||||
@@ -28,7 +28,7 @@ module Telegram
|
|||||||
options[:default] = defaults.flatten
|
options[:default] = defaults.flatten
|
||||||
key = "#{path}.#{action_name_i18n_key}#{key}"
|
key = "#{path}.#{action_name_i18n_key}#{key}"
|
||||||
end
|
end
|
||||||
I18n.translate(key, options)
|
I18n.translate(key, **options)
|
||||||
end
|
end
|
||||||
alias :t :translate
|
alias :t :translate
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
module Telegram
|
module Telegram
|
||||||
module Bot
|
module Bot
|
||||||
VERSION = '0.15.2'.freeze
|
VERSION = '0.15.3'.freeze
|
||||||
|
|
||||||
def self.gem_version
|
def self.gem_version
|
||||||
Gem::Version.new VERSION
|
Gem::Version.new VERSION
|
||||||
|
|||||||
@@ -1,5 +1,26 @@
|
|||||||
RSpec.describe Telegram::Bot::UpdatesController::Translation do
|
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
|
describe '#translate' do
|
||||||
|
stub_translations(
|
||||||
|
start: {
|
||||||
|
hello: 'Hello from start!',
|
||||||
|
hi: 'Hi %{name} from start', # rubocop:disable Style/FormatStringToken
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
let(:controller) do
|
let(:controller) do
|
||||||
double(
|
double(
|
||||||
controller_path: 'telegram/webhooks',
|
controller_path: 'telegram/webhooks',
|
||||||
@@ -10,12 +31,28 @@ RSpec.describe Telegram::Bot::UpdatesController::Translation do
|
|||||||
it 'uses action_name without ! for lazy translations' do
|
it 'uses action_name without ! for lazy translations' do
|
||||||
expect(I18n).to receive(:translate).with('telegram.webhooks.start.hello',
|
expect(I18n).to receive(:translate).with('telegram.webhooks.start.hello',
|
||||||
default: [:'telegram.webhooks.hello'],
|
default: [:'telegram.webhooks.hello'],
|
||||||
)
|
) { result }
|
||||||
controller.t('.hello')
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
describe described_class::ClassMethods do
|
describe described_class::ClassMethods do
|
||||||
|
stub_translations(
|
||||||
|
hello: 'Hello!',
|
||||||
|
hi: 'Hi %{name}', # rubocop:disable Style/FormatStringToken
|
||||||
|
)
|
||||||
|
|
||||||
describe '#translate' do
|
describe '#translate' do
|
||||||
let(:controller_class) do
|
let(:controller_class) do
|
||||||
double(controller_path: 'telegram/webhooks').
|
double(controller_path: 'telegram/webhooks').
|
||||||
@@ -23,8 +60,20 @@ RSpec.describe Telegram::Bot::UpdatesController::Translation do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it 'uses controller_path for lazy translations' do
|
it 'uses controller_path for lazy translations' do
|
||||||
expect(I18n).to receive(:translate).with('telegram.webhooks.hello', {})
|
expect(I18n).to receive(:translate) do |*args, **kwargs|
|
||||||
controller_class.t('.hello')
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user