Skip to content

Commit

Permalink
Added previous language switcher, pinwheel language translation
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeCodes19 committed Oct 1, 2024
1 parent 304265f commit e4a8012
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 31 deletions.
3 changes: 2 additions & 1 deletion app/app/controllers/api/pinwheel_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
15 changes: 14 additions & 1 deletion app/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/app/controllers/cbv/summaries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion app/app/helpers/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions app/app/javascript/controllers/cbv/employer_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 7 additions & 1 deletion app/app/javascript/utilities/pinwheel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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());
Expand Down
1 change: 0 additions & 1 deletion app/app/models/cbv_flow_invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/app/services/pinwheel_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 11 additions & 19 deletions app/app/views/application/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@
<% end %>
</em>
</div>

<nav class="usa-language">
<button type="button" class="usa-language__link">
<%= I18n.locale.to_s.upcase %>
</button>
<ul class="usa-language__submenu">
<% I18n.available_locales.each do |locale| %>
<% next if locale == I18n.locale %>
<li class="usa-language__submenu-item">
<%= link_to locale.to_s.upcase, url_for(locale: locale) %>
</li>
<% end %>
</ul>
</nav>

<% if user_signed_in? %>
<button class="usa-menu-btn"><%= t("shared.header.menu") %></button>
Expand All @@ -55,13 +41,19 @@
<button class="usa-nav__close">
<%= image_tag "@uswds/uswds/dist/img/usa-icons/close.svg", role: "img", alt: t("shared.header.close") %>
</button>
<% if user_signed_in? %>
<ul class="usa-nav__primary usa-accordion">
<li class="usa-nav__primary-item">
<%= button_to t("shared.header.log_out"), destroy_user_session_url(site_id: current_user.site_id), method: :delete, class: "usa-nav__link" %>
</li>
<% if user_signed_in? %>
<li class="usa-nav__primary-item">
<%= button_to t("shared.header.log_out"), destroy_user_session_url(site_id: current_user.site_id), method: :delete, class: "usa-nav__link" %>
</li>
<% end %>
<% I18n.available_locales.each do |locale| %>
<% next if locale == I18n.locale %>
<li class="usa-nav__primary-item">
<%= format_active_locale(locale) %>
</li>
<% end %>
</ul>
<% end %>
</nav>
</div>
</header>
6 changes: 4 additions & 2 deletions app/spec/controllers/cbv/summaries_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/spec/factories/cbv_flow_invitation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 { "[email protected]" }
Expand Down

0 comments on commit e4a8012

Please sign in to comment.