send a notification thanks to ActiveSupport::Notifications when rendering a page, a missing translation is found

did committed Jun 30, 2015
commit 81d6c0466fa9e0928171f3df48eec9a1f7f0c97e
Showing 3 changed files with 39 additions and 15 deletions
locomotive/steam/services/translator_service.rb b/lib/locomotive/steam/services/translator_service.rb +4 -1
@@ @@ -17,10 +17,13 @@ module Locomotive
if scope.blank?
values = repository.by_key(input).try(:values) || {}
- if translation = values[locale.to_s]
+ # FIXME: important to check if the returned value is nil (instead of nil + false)
+ # false being reserved for an existing key but without provided translation)
+ if (translation = values[locale.to_s]).present?
translation
else
Locomotive::Common::Logger.warn "Missing translation '#{input}' for the '#{locale}' locale".yellow
+ ActiveSupport::Notifications.instrument('steam.missing_translation', input: input, locale: locale)
input
end
else
spec/support/helpers.rb +13 -0
@@ @@ -43,5 +43,18 @@ module Spec
Rack::MockRequest.env_for(url, opts)
end
+ def notification_payload_for(notification)
+ payload = nil
+ subscription = ActiveSupport::Notifications.subscribe(notification) do |name, start, finish, id, _payload|
+ payload = _payload
+ end
+
+ yield
+
+ ActiveSupport::Notifications.unsubscribe(subscription)
+
+ return payload
+ end
+
end
end
spec/unit/services/translator_service_spec.rb +22 -14
@@ @@ -12,25 +12,18 @@ describe Locomotive::Steam::TranslatorService do
let(:locale) { nil }
let(:scope) { nil }
+ before do
+ allow(repository).to receive(:by_key).with('example_test').and_return(translation)
+ end
+
subject { service.translate(input, locale, scope) }
describe 'existing translation' do
let(:translation) { instance_double('Translation', values: { 'en' => 'Example text', 'es' => 'Texto de ejemplo' }) }
- before do
- allow(repository).to receive(:by_key).with('example_test').and_return(translation)
- end
-
it { is_expected.to eq 'Example text' }
- context 'no translation found' do
-
- let(:translation) { nil }
- it { is_expected.to eq 'example_test' }
-
- end
-
context 'specifying a locale' do
let(:locale) { 'es' }
@@ @@ -40,15 +33,15 @@ describe Locomotive::Steam::TranslatorService do
context "specifying a locale that doesn't exist" do
- let(:locale) { 'nl' }
+ let(:locale) { puts 'NL'; 'nl' }
it 'reverts to default locale' do
- is_expected.to eq "example_test"
+ is_expected.to eq 'example_test'
end
end
- context "specifying a scope" do
+ context 'specifying a scope' do
let(:input) { 'fr' }
let(:locale) { 'en' }
@@ @@ -60,6 +53,21 @@ describe Locomotive::Steam::TranslatorService do
end
+ describe 'missing translation' do
+
+ let(:locale) { 'fr' }
+ let(:translation) { nil }
+
+ it { is_expected.to eq 'example_test' }
+
+ it 'sends a notification' do
+ payload = notification_payload_for('steam.missing_translation') { subject }
+ expect(payload[:input]).to eq 'example_test'
+ expect(payload[:locale]).to eq 'fr'
+ end
+
+ end
+
end
end