From af787887d8eb4dc2cc8084f51258d1ed5c2901bb Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sat, 8 Jun 2024 23:02:29 +0300 Subject: [PATCH] i18n sign in --- app/controllers/application_controller.rb | 3 +- app/controllers/sessions_controller.rb | 2 + app/views/devise/sessions/new.html.erb | 14 +- app/views/devise/shared/_links.html.erb | 14 +- app/views/shared/_navbar.html.erb | 12 +- config/locales/en.yml | 268 ++++++++++++++++++++++ 6 files changed, 296 insertions(+), 17 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0b3dd112..e47840a3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -52,7 +52,8 @@ class ApplicationController < ActionController::Base private def with_browser_locale(&) - locale = request.env['HTTP_ACCEPT_LANGUAGE'].to_s[BROWSER_LOCALE_REGEXP].to_s + locale = params[:lang].presence + locale ||= request.env['HTTP_ACCEPT_LANGUAGE'].to_s[BROWSER_LOCALE_REGEXP].to_s locale = if locale.starts_with?('en-') && locale != 'en-US' diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e6fccfbf..0d51f41f 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,6 +3,8 @@ class SessionsController < Devise::SessionsController before_action :configure_permitted_parameters + around_action :with_browser_locale + def create email = sign_in_params[:email].to_s.downcase diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 43971e28..baca3e56 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -1,22 +1,22 @@
<%= render 'devise/shared/select_server' if Docuseal.multitenant? %> -

Sign In

+

<%= t('sign_in') %>

<%= form_for(resource, as: resource_name, html: { class: 'space-y-6' }, data: { turbo: params[:redir].blank? }, url: session_path(resource_name)) do |f| %> <% if params[:redir].present? %> <%= hidden_field_tag :redir, params[:redir] %> <% end %> -
+
- <%= f.label :email, class: 'label' %> + <%= f.label :email, t(:email), class: 'label' %> <%= f.email_field :email, autofocus: true, autocomplete: 'email', class: 'base-input' %>
- <%= f.label :password, class: 'label' %> + <%= f.label :password, t(:password), class: 'label' %> <%= f.password_field :password, autocomplete: 'current-password', class: 'base-input' %>
- <%= f.button button_title(title: 'Sign In', disabled_with: 'Signing In'), class: 'base-button' %> + <%= f.button button_title(title: t(:sign_in), disabled_with: t(:signing_in)), class: 'base-button' %>
<% end %> <% if devise_mapping.omniauthable? %> @@ -25,14 +25,14 @@ <%= form_for '', url: omniauth_authorize_path(resource_name, :google_oauth2), data: { turbo: false }, method: :post do |f| %> <%= hidden_field_tag :state, { redir: params[:redir].to_s }.compact_blank.to_query %> - <%= f.button button_title(title: 'Sign in with Google', icon: svg_icon('brand_google', class: 'w-6 h-6')), class: 'white-button w-full mt-4' %> + <%= f.button button_title(title: t('sign_in_with_google'), icon: svg_icon('brand_google', class: 'w-6 h-6')), class: 'white-button w-full mt-4' %> <% end %> <% end %> <% if User.omniauth_providers.include?(:microsoft_office365) %> <%= form_for '', url: omniauth_authorize_path(resource_name, :microsoft_office365), data: { turbo: false }, method: :post do |f| %> <%= hidden_field_tag :state, { redir: params[:redir].to_s }.compact_blank.to_query, id: 'state_microsoft' %> - <%= f.button button_title(title: 'Sign in with Microsoft', icon: svg_icon('brand_microsoft', class: 'w-6 h-6')), class: 'white-button w-full' %> + <%= f.button button_title(title: t('sign_in_with_microsoft'), icon: svg_icon('brand_microsoft', class: 'w-6 h-6')), class: 'white-button w-full' %> <% end %> <% end %>
diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb index 1399d39f..35ee5762 100644 --- a/app/views/devise/shared/_links.html.erb +++ b/app/views/devise/shared/_links.html.erb @@ -1,11 +1,19 @@
<%- if controller_name != 'sessions' %> - <%= link_to 'Already have an account?', new_session_path(resource_name), class: 'link link-hover mx-auto' %> + <%= link_to t('already_have_an_account'), new_session_path(resource_name, { lang: params[:lang] }.compact_blank), class: 'link link-hover mx-auto' %> <% end %> <%- if devise_mapping.registerable? && controller_name != 'registrations' %> - <%= link_to 'Create free account', registration_path({ redir: params[:redir] }.compact_blank), class: 'link link-hover' %> + <%= link_to t('create_free_account'), registration_path({ redir: params[:redir], lang: params[:lang] }.compact_blank), class: 'link link-hover' %> <% end %> <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> - <%= link_to 'Forgot your password?', new_password_path(resource_name), class: 'link link-hover' %> + <%= link_to t('forgot_your_password_'), new_password_path(resource_name, { lang: params[:lang] }.compact_blank), class: 'link link-hover' %> + <% end %> +
+
+ <%= form_with url: url_for, method: :get do %> + <% if params[:redir].present? %> + <%= hidden_field_tag :redir, params[:redir] %> + <% end %> + <%= select_tag :lang, options_for_select((I18n.available_locales - %i[en pt-PT de-DE fr-FR es-ES]).map { |code| [t("language_#{code}"), code] }, I18n.locale), onchange: 'this.form.requestSubmit();', class: 'select select-sm border-base-content/30 text-base' %> <% end %>
diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 2cd98733..7434b175 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -11,7 +11,7 @@
<% if Docuseal.demo? %> - Sign Up + <%= t('sign_up') %>