Add language settings for users, and translate titles to French
All checks were successful
CI / scan_ruby (push) Successful in 16s
CI / scan_js (push) Successful in 12s
CI / lint (push) Successful in 13s
CI / test (push) Successful in 36s

This commit is contained in:
sto 2025-03-27 12:55:12 +01:00
parent 497768610d
commit 8b0b1c6745
14 changed files with 128 additions and 3 deletions

View File

@ -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

View File

@ -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
View File

@ -0,0 +1,3 @@
module Languages
AVAILABLE_LANGUAGES = [ { id: "en", name: "English" }, { id: "fr", name: "French" } ]
end

View File

@ -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

View File

@ -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

View File

@ -14,6 +14,7 @@ module PuzzleScoreboard
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_paths << Rails.root.join("lib")
config.autoload_lib(ignore: %w[assets tasks])
# Configuration for the application, engines, and railties goes here.
@ -23,5 +24,8 @@ module PuzzleScoreboard
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.i18n.default_locale = :en
config.i18n.available_locales = [ :en, :fr ]
end
end

37
config/locales/fr.yml Normal file
View File

@ -0,0 +1,37 @@
fr:
completions:
edit:
title: "Modifier la complétion"
new:
title: "Nouvelle complétion"
contests:
edit:
title: "Paramètres du concours"
index:
title: "Bienvenue %{username}!"
new:
title: "Nouveau concours"
scoreboard:
title: "%{name}"
show:
title: "%{name}"
contestants:
edit:
title: "Participant"
new:
title: "Nouveau.elle participant.e"
puzzles:
edit:
title: "Modifier le puzzle"
new:
title: "Nouveau puzzle"
sessions:
new:
title: "Se connecter à l'app Public Scoreboard"
users:
edit:
title: "Mes paramètres"
index:
title: "Tous.tes les utilisateur.ices"
new:
title: "Nouveau.elle utilisateur.ice"

View File

@ -0,0 +1,5 @@
class AddLangToUser < ActiveRecord::Migration[8.0]
def change
add_column :users, :lang, :string, default: 'en'
end
end

3
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_03_26_162920) do
ActiveRecord::Schema[8.0].define(version: 2025_03_27_111835) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@ -111,6 +111,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_03_26_162920) do
t.datetime "updated_at", null: false
t.string "username"
t.boolean "admin", default: false, null: false
t.string "lang", default: "en"
t.index ["email_address"], name: "index_users_on_email_address", unique: true
end

View File

@ -1,3 +1,25 @@
# == Schema Information
#
# Table name: contests
#
# id :integer not null, primary key
# allow_registration :boolean default(FALSE)
# name :string
# slug :string
# team :boolean default(FALSE)
# created_at :datetime not null
# updated_at :datetime not null
# user_id :integer not null
#
# Indexes
#
# index_contests_on_slug (slug) UNIQUE
# index_contests_on_user_id (user_id)
#
# Foreign Keys
#
# user_id (user_id => users.id)
#
FactoryBot.define do
factory :contest do
name { Faker::Company.unique.name }

View File

@ -1,3 +1,20 @@
# == Schema Information
#
# Table name: users
#
# 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
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email_address (email_address) UNIQUE
#
FactoryBot.define do
factory :user do
username { Faker::Internet.unique.username }

View File

@ -1,3 +1,20 @@
# == Schema Information
#
# Table name: users
#
# 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
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email_address (email_address) UNIQUE
#
require 'rails_helper'
RSpec.describe User, type: :model do

View File

@ -8,6 +8,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

View File

@ -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