Skip to content

Commit

Permalink
Resolve #414 Add Language Support
Browse files Browse the repository at this point in the history
* Return question text and response text in English by default, but use the headers provided by ACCEPT_LANGUAGE to pick the available language.
  • Loading branch information
mzagaja committed Sep 27, 2023
1 parent 47e7db4 commit 83c5186
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'bootsnap', require: false
gem 'devise'
gem 'devise-jwt'
gem 'faraday'
gem 'http_accept_language'
gem 'jbuilder'
gem 'newrelic_rpm'
gem 'pg', '~> 1.1'
Expand Down
2 changes: 2 additions & 0 deletions backend/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ GEM
faraday-net_http (3.0.2)
globalid (1.1.0)
activesupport (>= 5.0)
http_accept_language (2.1.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
Expand Down Expand Up @@ -287,6 +288,7 @@ DEPENDENCIES
devise-jwt
factory_bot_rails
faraday
http_accept_language
jbuilder
newrelic_rpm
pg (~> 1.1)
Expand Down
5 changes: 4 additions & 1 deletion backend/app/controllers/surveys_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class SurveysController < ApplicationController
AVAILABLE_LANGUAGES = %w(en es)
before_action :set_survey, only: %i[show edit update destroy]

# GET /surveys or /surveys.json
Expand All @@ -9,7 +10,9 @@ def index
end

# GET /surveys/1 or /surveys/1.json
def show; end
def show
@language = http_accept_language.preferred_language_from(AVAILABLE_LANGUAGES)
end

# GET /surveys/new
def new
Expand Down
5 changes: 5 additions & 0 deletions backend/app/models/localized_survey_question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class LocalizedSurveyQuestion < ApplicationRecord
belongs_to :survey_question
end
14 changes: 14 additions & 0 deletions backend/app/models/survey_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
class SurveyQuestion < ApplicationRecord
belongs_to :survey
enum response_type: { radio: 0, text: 1 }
has_many :localized_survey_questions
accepts_nested_attributes_for :localized_survey_questions

validates :display_order, uniqueness: { scope: :survey_id }

after_initialize do
@language ||= 'en'
end

def text
localized_survey_questions.find_by(language_code: @language_code).text
end

def response_options
localized_survey_questions.find_by(language_code: @language_code).response_options
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateLocalizedSurveyQuestions < ActiveRecord::Migration[7.0]
def change
create_table :localized_survey_questions do |t|
t.string :language_code
t.text :text
t.string :response_options, array: true
t.references :survey_question

t.timestamps
end
remove_column :survey_questions, :text, :text
remove_column :survey_questions, :response_options, :string, array: true
end
end
19 changes: 13 additions & 6 deletions backend/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion backend/db/seed_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ def import_seed_data(path, clustered_ordered_parcels)
chunk.each do |data_hash|
data_hash[:response_options] =
(data_hash[:response_options].nil? ? [] : data_hash[:response_options].split('/', -1))
question = SurveyQuestion.new(data_hash)
fixed_data_hash = {
display_order: data_hash[:display_order],
response_type: data_hash[:response_type],
localized_survey_questions: [
LocalizedSurveyQuestion.new({
text: data_hash[:text],
response_options: data_hash[:response_options],
language_code: 'en'
})]
}
question = SurveyQuestion.new(fixed_data_hash)
question.survey = survey
question.save!
end
Expand Down

0 comments on commit 83c5186

Please sign in to comment.