Skip to content

Commit

Permalink
Merge pull request #917 from thecartercenter/12241_fix_multilingual_m…
Browse files Browse the repository at this point in the history
…ultilevel_csv

12241 fix multilingual multilevel csv export in non-default language
  • Loading branch information
cooperka authored Jul 21, 2022
2 parents 805f06b + f24d48a commit 50d506a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/models/results/csv/header_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def add_location_headers(code, lat_lng_only: false)

def add_level_headers(code, level_names)
JSON.parse(level_names).each do |level|
key = locales.detect { |l| level[l.to_s].present? } || level.keys.first
# Check every locale, starting with user preference, to see where the level has a real name defined.
key = ([I18n.locale] + locales).detect { |l| level[l.to_s].present? } || level.keys.first
add(code, suffix: level[key.to_s])
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/response_csv/multilingual_cascading.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ResponseID,Shortcode,Form,Submitter,DateSubmitted,Reviewed,GroupName,GroupLevel,SelectOneQ1:Royaume,SelectOneQ1:Espèce
*id1*,*shortcode1*,Sample Form 1,A User 1,2015-11-20 06:30:00 -0600,false,,0,,
*id1*,*shortcode1*,Sample Form 1,A User 1,2015-11-20 06:30:00 -0600,false,Groupe,1,Animale,Chat
3 changes: 3 additions & 0 deletions spec/fixtures/response_csv/multilingual_cascading_en.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ResponseID,Shortcode,Form,Submitter,DateSubmitted,Reviewed,GroupName,GroupLevel,SelectOneQ1:Kingdom,SelectOneQ1:Species
*id1*,*shortcode1*,Sample Form 1,A User 1,2015-11-20 06:30:00 -0600,false,,0,,
*id1*,*shortcode1*,Sample Form 1,A User 1,2015-11-20 06:30:00 -0600,false,Group 1,1,Animal,Cat
60 changes: 60 additions & 0 deletions spec/models/results/csv/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,66 @@
end
end

context "with multilingual cascading selects" do
let(:form) { create(:form, question_types: [{repeating: {items: ["multilevel_select_one"]}}]) }
let(:group) { form.c[0] }
let(:option_set) { form.c[0].c[0].option_set }
let(:option1) { option_set.c[0].option }
let(:option2) { option_set.c[0].c[0].option }

before do
# Avoid missing translation errors for headers. We're not testing those here as
# those are picked up with standard I18n.translate. In production this isn't an issue
# because fallbacks are enabled.
I18n.backend.store_translations(:fr, response: {csv_headers: I18n.t("response.csv_headers")})

get_mission.setting.update!(preferred_locales_str: "fr,en")
I18n.locale = :fr
group.update!(group_name_fr: "Groupe")
option_set.update!(level_names: [{en: "Kingdom", fr: "Royaume"}, {en: "Species", fr: "Espèce"}])
option1.update!(name_fr: "Animale")
option2.update!(name_fr: "Chat")

Timecop.freeze(submission_time) do
create_response(form: form, answer_values: [{repeating: [[[option1.name_en, option2.name_en]]]}])
end
end

it "uses french names when appropriate" do
is_expected.to match_user_facing_csv(prepare_response_csv_expectation("multilingual_cascading.csv"))
end
end

context "with multilingual cascading selects in non-default locale" do
let(:form) { create(:form, question_types: [{repeating: {items: ["multilevel_select_one"]}}]) }
let(:group) { form.c[0] }
let(:option_set) { form.c[0].c[0].option_set }
let(:option1) { option_set.c[0].option }
let(:option2) { option_set.c[0].c[0].option }

before do
# Avoid missing translation errors for headers. We're not testing those here as
# those are picked up with standard I18n.translate. In production this isn't an issue
# because fallbacks are enabled.
I18n.backend.store_translations(:fr, response: {csv_headers: I18n.t("response.csv_headers")})

get_mission.setting.update!(preferred_locales_str: "fr,en")
I18n.locale = :en # Non-default locale
group.update!(group_name_fr: "Groupe")
option_set.update!(level_names: [{en: "Kingdom", fr: "Royaume"}, {en: "Species", fr: "Espèce"}])
option1.update!(name_fr: "Animale")
option2.update!(name_fr: "Chat")

Timecop.freeze(submission_time) do
create_response(form: form, answer_values: [{repeating: [[[option1.name_en, option2.name_en]]]}])
end
end

it "uses french names when appropriate" do
is_expected.to match_user_facing_csv(prepare_response_csv_expectation("multilingual_cascading_en.csv"))
end
end

context "with numeric values for select_one and select_multiple questions" do
let(:form) { create(:form, question_types: %w[select_one select_multiple]) }

Expand Down

0 comments on commit 50d506a

Please sign in to comment.