diff --git a/app/app/controllers/api/pinwheel_controller.rb b/app/app/controllers/api/pinwheel_controller.rb index a7ed802a..2dc2dff2 100644 --- a/app/app/controllers/api/pinwheel_controller.rb +++ b/app/app/controllers/api/pinwheel_controller.rb @@ -8,7 +8,8 @@ def create_token token_response = pinwheel.create_link_token( response_type: token_params[:response_type], id: token_params[:id], - end_user_id: @cbv_flow.pinwheel_end_user_id + end_user_id: @cbv_flow.pinwheel_end_user_id, + language: session[:locale] ) token = token_response["data"]["token"] diff --git a/app/app/controllers/application_controller.rb b/app/app/controllers/application_controller.rb index 9a6444ad..50823750 100644 --- a/app/app/controllers/application_controller.rb +++ b/app/app/controllers/application_controller.rb @@ -18,8 +18,9 @@ def after_sign_out_path_for(resource_or_scope) end def switch_locale(&action) - locale = params[:locale] || I18n.default_locale + locale = get_locale(request) I18n.with_locale(locale, &action) + session[:locale] = locale end def site_config @@ -28,6 +29,18 @@ def site_config private + def get_locale(request) + locale_sources = [ + params[:locale], + request.path.split("/")[1], + URI(request.env["HTTP_REFERER"]).path.split("/")[1], + request.env["HTTP_ACCEPT_LANGUAGE"]&.scan(/^[a-z]{2}/)&.first, + I18n.default_locale + ] + + locale_sources.compact.find { |locale| I18n.available_locales.map(&:to_s).include?(locale) } + end + def current_site @current_site ||= site_config[params[:site_id]] end diff --git a/app/app/controllers/cbv/summaries_controller.rb b/app/app/controllers/cbv/summaries_controller.rb index fd12ba58..57019f25 100644 --- a/app/app/controllers/cbv/summaries_controller.rb +++ b/app/app/controllers/cbv/summaries_controller.rb @@ -191,7 +191,8 @@ def track_transmitted_event(cbv_flow, payments) paystub_count: payments.count, account_count_with_additional_information: cbv_flow.additional_information.values.count { |info| info["comment"].present? }, - flow_started_seconds_ago: (Time.now - cbv_flow.created_at).to_i + flow_started_seconds_ago: (Time.now - cbv_flow.created_at).to_i, + language: I18n.locale }) rescue => ex Rails.logger.error "Failed to track NewRelic event: #{ex.message}" diff --git a/app/app/helpers/view_helper.rb b/app/app/helpers/view_helper.rb index 9765bd0e..00334b5f 100644 --- a/app/app/helpers/view_helper.rb +++ b/app/app/helpers/view_helper.rb @@ -6,7 +6,7 @@ def format_active_locale(locale_string) if locale_string.to_sym == I18n.locale link_classes = "#{link_classes} usa-current" end - link_to t("shared.languages.#{locale_string}"), root_path(locale: locale_string), class: link_classes + link_to t("shared.languages.#{locale_string}"), url_for(locale: locale_string), class: link_classes end def format_parsed_date(date) diff --git a/app/app/javascript/controllers/cbv/employer_search.js b/app/app/javascript/controllers/cbv/employer_search.js index 1b0c8a97..a9e43a48 100644 --- a/app/app/javascript/controllers/cbv/employer_search.js +++ b/app/app/javascript/controllers/cbv/employer_search.js @@ -61,11 +61,15 @@ export default class extends Controller { } } + getDocumentLocale() { + return document.documentElement.lang + } + async select(event) { const { responseType, id } = event.target.dataset; this.disableButtons() - - const { token } = await fetchToken(responseType, id); + const locale = this.getDocumentLocale(); + const { token } = await fetchToken(responseType, id, locale); this.submit(token); } diff --git a/app/app/javascript/utilities/pinwheel.js b/app/app/javascript/utilities/pinwheel.js index 78fccda9..36db6155 100644 --- a/app/app/javascript/utilities/pinwheel.js +++ b/app/app/javascript/utilities/pinwheel.js @@ -3,6 +3,11 @@ import metaContent from "./meta"; import CSRF from './csrf'; const PINWHEEL_TOKENS_GENERATE = '/api/pinwheel/tokens'; +const resolveLanguage = (locale) => { + const enLanguageHeader = 'en,en-US;q=0.9,es;q=0.8,es-ES;q=0.7,es-MX;q=0.6' + const esLanguageHeader = 'es,es-ES;q=0.9,es-MX;q=0.8,en-US;q=0.7,en;q=0.6' + return locale === 'en' ? enLanguageHeader : esLanguageHeader +} export function loadPinwheel() { return new Promise((resolve, reject) => { @@ -25,12 +30,13 @@ export function initializePinwheel(Pinwheel, linkToken, callbacks) { return Pinwheel; } -export const fetchToken = (response_type, id) => { +export const fetchToken = (response_type, id, locale) => { return fetch(PINWHEEL_TOKENS_GENERATE, { method: 'post', headers: { 'X-CSRF-Token': CSRF.token, 'Content-Type': 'application/json', + 'Accept-Language': resolveLanguage(locale), }, body: JSON.stringify({ response_type, id }), }).then(response => response.json()); diff --git a/app/app/models/cbv_flow_invitation.rb b/app/app/models/cbv_flow_invitation.rb index cba23df4..2e474985 100644 --- a/app/app/models/cbv_flow_invitation.rb +++ b/app/app/models/cbv_flow_invitation.rb @@ -27,7 +27,6 @@ class CbvFlowInvitation < ApplicationRecord PAYSTUB_REPORT_RANGE = 90.days VALID_LANGUAGES = Rails.application.config.i18n.available_locales.map(&:to_s).freeze - puts "VALID_LANGUAGES: #{VALID_LANGUAGES}" belongs_to :user has_many :cbv_flows diff --git a/app/app/services/pinwheel_service.rb b/app/app/services/pinwheel_service.rb index a83f724b..34bf7f8c 100644 --- a/app/app/services/pinwheel_service.rb +++ b/app/app/services/pinwheel_service.rb @@ -88,12 +88,13 @@ def fetch_platform(platform_id:) @http.get(build_url("#{PLATFORMS_ENDPOINT}/#{platform_id}")).body end - def create_link_token(end_user_id:, response_type:, id:) + def create_link_token(end_user_id:, response_type:, id:, language:) params = { org_name: I18n.t("shared.pilot_name"), required_jobs: [ "paystubs" ], end_user_id: end_user_id, - skip_intro_screen: true + skip_intro_screen: true, + language: language } case response_type.presence diff --git a/app/app/views/application/_header.html.erb b/app/app/views/application/_header.html.erb index 9160aefa..7024c376 100644 --- a/app/app/views/application/_header.html.erb +++ b/app/app/views/application/_header.html.erb @@ -32,20 +32,6 @@ <% end %> - - <% if user_signed_in? %> @@ -55,13 +41,19 @@ - <% if user_signed_in? %> - <% end %> diff --git a/app/spec/controllers/cbv/summaries_controller_spec.rb b/app/spec/controllers/cbv/summaries_controller_spec.rb index 91c7f1a5..86d2ca45 100644 --- a/app/spec/controllers/cbv/summaries_controller_spec.rb +++ b/app/spec/controllers/cbv/summaries_controller_spec.rb @@ -239,7 +239,8 @@ account_count: 1, paystub_count: 1, account_count_with_additional_information: 0, - flow_started_seconds_ago: flow_started_seconds_ago + flow_started_seconds_ago: flow_started_seconds_ago, + language: "en".to_sym }) end end @@ -319,7 +320,8 @@ account_count: 1, paystub_count: 1, account_count_with_additional_information: 0, - flow_started_seconds_ago: flow_started_seconds_ago + flow_started_seconds_ago: flow_started_seconds_ago, + language: "en".to_sym }) end end diff --git a/app/spec/factories/cbv_flow_invitation.rb b/app/spec/factories/cbv_flow_invitation.rb index 320eb845..e442281e 100644 --- a/app/spec/factories/cbv_flow_invitation.rb +++ b/app/spec/factories/cbv_flow_invitation.rb @@ -2,6 +2,7 @@ factory :cbv_flow_invitation, class: "CbvFlowInvitation" do first_name { "Jane" } middle_name { "Sue" } + language { "en".to_sym } last_name { "Doe" } site_id { "sandbox" } email_address { "test@example.com" }