Skip to content

Commit

Permalink
Allow guest users to select their locale
Browse files Browse the repository at this point in the history
This commit makes it so that guest users can select their locale
from the locale selector as if they were logged in.

Moreover, guest users' locale on the site will now be automatically
determined based on their browser settings in case they haven't
selected one themselves.
  • Loading branch information
BGMP committed Mar 19, 2024
1 parent 769814c commit 5f9ce0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
19 changes: 14 additions & 5 deletions app/controllers/application_controller.rb
Expand Up @@ -6,11 +6,20 @@ class ApplicationController < ActionController::Base
before_action :build_navigation

def set_locale
I18n.locale = if user_signed_in?
current_user.locale
else
I18n.default_locale
end
if user_signed_in?
I18n.locale = current_user.locale
else
I18n.locale = params[:locale] || locale_from_header || I18n.default_locale
end
end

def default_url_options
{ :locale => I18n.locale }
end

# Detect Browser's locale and use that by default
def locale_from_header
request.env.fetch('HTTP_ACCEPT_LANGUAGE', '').scan(/[a-z]{2}/).first
end

def build_navigation
Expand Down
13 changes: 13 additions & 0 deletions app/views/application/_locale_selector.haml
@@ -0,0 +1,13 @@
%li.dropdown
%a.nav-link.dropdown-toggle{:href => "", :"data-toggle" => "dropdown", :"aria-haspopup" => true, :"aria-expanded" => false}
= image_tag "i18n/#{I18n.locale}.png", :class => "locale-flag"
%ul.dropdown-menu.dropdown-menu-right{:"aria-labelledby" => "dropdown02"}
- I18n.available_locales.each do |l|
- if user_signed_in?
%a.dropdown-item{:"data-turbo" => "true", :"data-turbo-method" => "put", :href => users_locale_path(:locale => l)}
= image_tag "i18n/#{l}.png", :class => "locale-flag"
= SYS::LOCALES_MAP.key(l)
- else
%a.dropdown-item{:href => "?locale=#{l}"}
= image_tag "i18n/#{l}.png", :class => "locale-flag"
= SYS::LOCALES_MAP.key(l)
12 changes: 2 additions & 10 deletions app/views/application/_nav.haml
@@ -1,6 +1,6 @@
%nav.navbar.navbar-expand-md.navbar-dark.navbar-default.bg-obscure
%div.container
%a.navbar-brand{:href => "/"}
%a.navbar-brand{:href => root_path}
= image_tag "rva-logo-dark.png", :height => "45", :width => "45", :style => "border-radius: 50px;"

%button.navbar-toggler{:type => "button", :"data-toggle" => "collapse", :"data-target" => "#main-navbar", :"aria-controls" => "main-navbar", :"aria-expanded" => "false", :"aria-label" => "Toggle navigation"}
Expand Down Expand Up @@ -47,15 +47,7 @@
= t('nav.downloads')
%li.nav-item
%ul.navbar-nav
- if user_signed_in?
%li.dropdown
%a.nav-link.dropdown-toggle{:href => "", :"data-toggle" => "dropdown", :"aria-haspopup" => true, :"aria-expanded" => false}
= image_tag "i18n/#{I18n.locale}.png", :class => "locale-flag"
%ul.dropdown-menu.dropdown-menu-right{:"aria-labelledby" => "dropdown02"}
- I18n.available_locales.each do |l|
%a.dropdown-item{:"data-turbo" => "true", :"data-turbo-method" => "put", :href => users_locale_path(:locale => l)}
= image_tag "i18n/#{l}.png", :class => "locale-flag"
= SYS::LOCALES_MAP.key(l)
= render :partial => 'locale_selector'

%li.dropdown
%a.nav-link.dropdown-toggle{:id => "access-toggle", :"data-toggle" => "dropdown", :href => "#"}
Expand Down

0 comments on commit 5f9ce0d

Please sign in to comment.