From 9a502c73c99d016e59c92a1513d4b8b7ca054237 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Thu, 29 Sep 2016 18:55:41 -0300 Subject: [PATCH 1/3] FIX: Properly localize badge notification on batch grant --- app/services/badge_granter.rb | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index da74a099746..b5206b61b99 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -274,7 +274,7 @@ class BadgeGranter /*where*/ RETURNING id, user_id, granted_at ) - select w.*, username FROM w + select w.*, username, locale FROM w JOIN users u on u.id = w.user_id " @@ -305,15 +305,24 @@ class BadgeGranter # old bronze badges do not matter next if badge.badge_type_id == BadgeType::Bronze and row.granted_at < 2.days.ago - notification = Notification.create!( - user_id: row.user_id, - notification_type: Notification.types[:granted_badge], - data: { - badge_id: badge.id, - badge_name: badge.name, - badge_slug: badge.slug, - username: row.username - }.to_json ) + # Try to use user locale in the badge notification if possible without too much resources + notification_locale = if SiteSetting.allow_user_locale && row.locale.present? + row.locale + else + SiteSetting.default_locale + end + + I18n.with_locale(notification_locale) do + notification = Notification.create!( + user_id: row.user_id, + notification_type: Notification.types[:granted_badge], + data: { + badge_id: badge.id, + badge_name: badge.display_name, + badge_slug: badge.slug, + username: row.username + }.to_json ) + end Badge.exec_sql("UPDATE user_badges SET notification_id = :notification_id WHERE id = :id", notification_id: notification.id, From c5b94878ac75f7dc69cdd3addc81db2b6d8f4360 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 11 Oct 2016 19:14:32 -0300 Subject: [PATCH 2/3] We need this variable later --- app/services/badge_granter.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/services/badge_granter.rb b/app/services/badge_granter.rb index b5206b61b99..ece099e4f1f 100644 --- a/app/services/badge_granter.rb +++ b/app/services/badge_granter.rb @@ -312,6 +312,9 @@ class BadgeGranter SiteSetting.default_locale end + # Make this variable in this scope + notification = nil + I18n.with_locale(notification_locale) do notification = Notification.create!( user_id: row.user_id, From 48fa1f141fdb5469f6ba0f6e2c5c7a9d4aaf9920 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 11 Oct 2016 19:15:36 -0300 Subject: [PATCH 3/3] Add specs for localized notifications on backfilled badges --- spec/services/badge_granter_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb index a7ce5e40df0..cb906c282d5 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -94,6 +94,23 @@ describe BadgeGranter do expect(Badge.find(Badge::NiceTopic).grant_count).to eq(1) expect(Badge.find(Badge::GoodTopic).grant_count).to eq(1) end + + it 'should grant badges in the user locale' do + + SiteSetting.allow_user_locale = true + + nice_topic = Badge.find(Badge::NiceTopic) + name_english = nice_topic.name + + user = Fabricate(:user, locale: 'fr') + post = Fabricate(:post, like_count: 10, user: user) + + BadgeGranter.backfill(nice_topic) + + notification_badge_name = JSON.parse(post.user.notifications.first.data)['badge_name'] + + expect(notification_badge_name).not_to eq(name_english) + end end describe 'grant' do