Skip to content

Commit

Permalink
Add select language field
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Vasquez committed Mar 31, 2022
1 parent 7565c20 commit de6fccd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
15 changes: 11 additions & 4 deletions engine/app/controllers/good_job/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module GoodJob
class BaseController < ActionController::Base # rubocop:disable Rails/ApplicationController
protect_from_forgery with: :exception

around_action :switch_locale
before_action :set_locale

content_security_policy do |policy|
policy.default_src(:none) if policy.default_src(*policy.default_src).blank?
Expand All @@ -24,11 +24,18 @@ class BaseController < ActionController::Base # rubocop:disable Rails/Applicatio
request.content_security_policy_nonce_generator = ->(_request) { SecureRandom.base64(16) }
end

def default_url_options(options = {})
{ locale: I18n.locale }.merge(options)
end

private

def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
def set_locale
I18n.locale = current_locale
end

def current_locale
params[:locale] || I18n.default_locale
end
end
end
12 changes: 12 additions & 0 deletions engine/app/helpers/good_job/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,17 @@ def status_badge(status)

content_tag :span, status.to_s, class: classes
end

def language_options
available_languages.map { |locale| language_option(locale) }.join
end

def language_option(locale)
link_to locale, url_for(locale: locale), class: "dropdown-item"
end

def available_languages
I18n.available_locales.reject { |locale| locale == I18n.locale }
end
end
end
18 changes: 15 additions & 3 deletions engine/app/views/good_job/shared/_navbar.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@
</div>
</li>
</ul>
<div>
<input type="checkbox" id="toggle-poll" name="toggle-poll" data-gj-action='change#togglePoll' <%= 'checked' if params[:poll].present? %>>
<label for="toggle-poll">Live Poll</label>
<div class="navbar-nav">
<div class="nav-item pe-2">
<input type="checkbox" id="toggle-poll" name="toggle-poll" data-gj-action='change#togglePoll' <%= 'checked' if params[:poll].present? %>>
<label for="toggle-poll">Live Poll</label>
</div>
<div class="nav-item">
<div class="dropdown">
<button class="btn btn-primary btn-sm dropdown-toggle" type="button" id="localeOptions" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<%= I18n.locale %>
</button>
<div class="dropdown-menu" aria-labelledby="localeOptions">
<%== language_options %>
</div>
</div>
</div>
</div>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions engine/config/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true
module GoodJob
class Application < Rails::Application
config.i18n.available_locales = [:en, :es]
config.i18n.default_locale = :en
end
end
25 changes: 9 additions & 16 deletions spec/system/dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,16 @@
end
end

describe "renders /good_job in Spanish lang" do
it "renders the navbar successfully" do
visit "/good_job?locale=es"

expect(page).to have_content "GoodJob 👍"
expect(page).to have_content "Ejecuciones"
expect(page).to have_content "Tareas"
expect(page).to have_content "Tareas Programadas"
describe "when changing language" do
it "changes wording from English to Spanish" do
visit good_job_path(locale: :en)

expect(page).to have_content "Processes"
find("#localeOptions").click
within "div[aria-labelledby='localeOptions']" do
click_on "es"
end
expect(page).to have_content "Procesos"
expect(page).to have_content "Próximamente más vistas"
end

it "renders work_in_progress successfully" do
visit "/good_job?locale=es"

expect(page).to have_content "🚧 GoodJob se encuentra en desarrollo."
expect(page).to have_content "Por favor contribuya en GoodJob con ideas y código."
end
end
end

0 comments on commit de6fccd

Please sign in to comment.