Add language settings for users, and translate titles to French
This commit is contained in:
@@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base
|
||||
include Authentication
|
||||
include Pundit::Authorization
|
||||
|
||||
before_action :set_title, :set_current_user
|
||||
before_action :set_title, :set_current_user, :set_lang
|
||||
after_action :verify_authorized
|
||||
|
||||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
|
||||
@@ -23,6 +23,10 @@ class ApplicationController < ActionController::Base
|
||||
@current_user = current_user
|
||||
end
|
||||
|
||||
def set_lang
|
||||
I18n.locale = @current_user.lang if @current_user
|
||||
end
|
||||
|
||||
def user_not_authorized(exception)
|
||||
policy_name = exception.policy.class.to_s.underscore
|
||||
|
||||
|
@@ -55,6 +55,6 @@ class UsersController < ApplicationController
|
||||
end
|
||||
|
||||
def user_params
|
||||
params.expect(user: [ :username, :email_address, :password ])
|
||||
params.expect(user: [ :username, :email_address, :lang, :password ])
|
||||
end
|
||||
end
|
||||
|
3
app/lib/languages.rb
Normal file
3
app/lib/languages.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
module Languages
|
||||
AVAILABLE_LANGUAGES = [ { id: "en", name: "English" }, { id: "fr", name: "French" } ]
|
||||
end
|
@@ -5,6 +5,7 @@
|
||||
# id :integer not null, primary key
|
||||
# admin :boolean default(FALSE), not null
|
||||
# email_address :string not null
|
||||
# lang :string default("en")
|
||||
# password_digest :string not null
|
||||
# username :string
|
||||
# created_at :datetime not null
|
||||
@@ -22,4 +23,5 @@ class User < ApplicationRecord
|
||||
normalizes :email_address, with: ->(e) { e.strip.downcase }
|
||||
|
||||
validates :username, presence: true, uniqueness: true
|
||||
validates :lang, inclusion: { in: Languages::AVAILABLE_LANGUAGES.map { |lang| lang[:id] } }
|
||||
end
|
||||
|
@@ -1,6 +1,7 @@
|
||||
= form_with model: user, method: method do |form|
|
||||
- if method == :patch
|
||||
h4 General settings
|
||||
|
||||
.row.mb-3
|
||||
.col
|
||||
.input-group
|
||||
@@ -8,11 +9,20 @@
|
||||
.form-floating
|
||||
= form.text_field :username, autocomplete: "off", class: "form-control"
|
||||
= form.label :username, class: "required"
|
||||
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
= form.text_field :email_address, autocomplete: "off", class: "form-control"
|
||||
= form.label :email_address, class: "required"
|
||||
|
||||
.row.mb-3
|
||||
.col
|
||||
.form-floating
|
||||
= form.select :lang, Languages::AVAILABLE_LANGUAGES.map { |lang| [ lang[:name], lang[:id] ] }, {}, class: "form-select"
|
||||
= form.label :lang
|
||||
| Language
|
||||
|
||||
- if method == :post
|
||||
.row.mb-3
|
||||
.col
|
||||
@@ -23,6 +33,7 @@
|
||||
|
||||
- if method == :patch
|
||||
h4.mt-5 Change password
|
||||
|
||||
= form_with model: user, method: method do |form|
|
||||
.row.mb-3
|
||||
.col
|
||||
|
Reference in New Issue
Block a user