Skip to content

Commit

Permalink
Add spec coverage for missing translations handling
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Jan 3, 2024
1 parent 56be6b4 commit e75f10b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion admin/app/components/solidus_admin/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def missing_translation(key, options)

logger.debug " [#{self.class}] Missing translation: #{keys.join('.')}"

if options[:locale] != :en
if (options[:locale] || I18n.default_locale) != :en
t(key, **options, locale: :en)
else
"translation missing: #{keys.join('.')}"
Expand Down
2 changes: 1 addition & 1 deletion admin/config/initializers/view_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
clear = "\e[0m"

ActiveSupport::Notifications.subscribe("render.view_component") do |*args|
next unless args.last[:name].starts_with?("SolidusAdmin::")
next unless args.last[:name]&.starts_with?("SolidusAdmin::")

event = ActiveSupport::Notifications::Event.new(*args)
SolidusAdmin::BaseComponent.logger.debug \
Expand Down
16 changes: 16 additions & 0 deletions admin/spec/components/solidus_admin/base_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ def call
expect(SolidusAdmin::Foo::Bar::Component.new.stimulus_id).to eq("foo--bar")
end
end

describe "missing translations" do
it "logs and shows the full chain of keys" do
debug_logs = []

allow(Rails.logger).to receive(:debug) { debug_logs << _1 }

component_class = stub_const("Foo::Component", Class.new(described_class){ erb_template "" })
component = component_class.new
render_inline(component)
translation = component.translate("foo.bar.baz")

expect(translation).to eq("translation missing: en.foo.bar.baz")
expect(debug_logs).to include(%{ [Foo::Component] Missing translation: en.foo.bar.baz})
end
end
end

0 comments on commit e75f10b

Please sign in to comment.