From e75f10b7ab1d814a94fab9e3552deec2103082aa Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Wed, 3 Jan 2024 13:28:37 +0100 Subject: [PATCH] Add spec coverage for missing translations handling --- .../components/solidus_admin/base_component.rb | 2 +- admin/config/initializers/view_component.rb | 2 +- .../solidus_admin/base_component_spec.rb | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/admin/app/components/solidus_admin/base_component.rb b/admin/app/components/solidus_admin/base_component.rb index 6e5483e9e98..f2d524e2ee8 100644 --- a/admin/app/components/solidus_admin/base_component.rb +++ b/admin/app/components/solidus_admin/base_component.rb @@ -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('.')}" diff --git a/admin/config/initializers/view_component.rb b/admin/config/initializers/view_component.rb index 5222819a7db..beb9345c26a 100644 --- a/admin/config/initializers/view_component.rb +++ b/admin/config/initializers/view_component.rb @@ -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 \ diff --git a/admin/spec/components/solidus_admin/base_component_spec.rb b/admin/spec/components/solidus_admin/base_component_spec.rb index dc2ab173aa9..2c95f2c9fd3 100644 --- a/admin/spec/components/solidus_admin/base_component_spec.rb +++ b/admin/spec/components/solidus_admin/base_component_spec.rb @@ -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