From 497768610dc98338078d24f7e0d234ced0c1ba48 Mon Sep 17 00:00:00 2001 From: sto Date: Thu, 27 Mar 2025 12:15:27 +0100 Subject: [PATCH] Setup I18n for titles --- app/controllers/application_controller.rb | 5 ++- app/controllers/completions_controller.rb | 5 --- app/controllers/contestants_controller.rb | 5 --- app/controllers/contests_controller.rb | 9 ++---- app/controllers/puzzles_controller.rb | 5 --- app/controllers/sessions_controller.rb | 1 - app/controllers/users_controller.rb | 5 --- config/locales/en.yml | 37 ++++++++++++++++++++++- spec/features/login_spec.rb | 6 ++-- spec/features/user_spec.rb | 10 +++--- 10 files changed, 51 insertions(+), 37 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e897995..dadab43 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,7 +13,10 @@ class ApplicationController < ActionController::Base private def set_title - @title = "Public scoreboard" + t_action_name = action_name + t_action_name = "new" if action_name == "create" + t_action_name = "edit" if action_name == "update" + @title = I18n.t("#{controller_name}.#{t_action_name}.title") end def set_current_user diff --git a/app/controllers/completions_controller.rb b/app/controllers/completions_controller.rb index cbd9b57..6281620 100644 --- a/app/controllers/completions_controller.rb +++ b/app/controllers/completions_controller.rb @@ -7,8 +7,6 @@ class CompletionsController < ApplicationController def edit authorize @contest - - @title = "Edit completion" end def new @@ -18,7 +16,6 @@ class CompletionsController < ApplicationController if params[:contestant_id] @completion.contestant_id = params[:contestant_id] end - @title = "New completion" end def create @@ -32,7 +29,6 @@ class CompletionsController < ApplicationController else logger = Logger.new(STDOUT) logger.info(@completion.errors) - @title = "New completion" render :new, status: :unprocessable_entity end end @@ -47,7 +43,6 @@ class CompletionsController < ApplicationController extend_completions!(@completion.contestant) redirect_to @contest else - @title = "Edit completion" render :edit, status: :unprocessable_entity end end diff --git a/app/controllers/contestants_controller.rb b/app/controllers/contestants_controller.rb index 444acc2..5422292 100644 --- a/app/controllers/contestants_controller.rb +++ b/app/controllers/contestants_controller.rb @@ -5,15 +5,12 @@ class ContestantsController < ApplicationController def edit authorize @contest - - @title = "Contestant" end def new authorize @contest @contestant = Contestant.new - @title = "New contestant" end def create @@ -24,7 +21,6 @@ class ContestantsController < ApplicationController if @contestant.save redirect_to contest_path(@contest) else - @title = "New contestant" render :new, status: :unprocessable_entity end end @@ -35,7 +31,6 @@ class ContestantsController < ApplicationController if @contestant.update(contestant_params) redirect_to @contest else - @title = "Contestant" render :edit, status: :unprocessable_entity end end diff --git a/app/controllers/contests_controller.rb b/app/controllers/contests_controller.rb index af758cc..8f30774 100644 --- a/app/controllers/contests_controller.rb +++ b/app/controllers/contests_controller.rb @@ -6,13 +6,13 @@ class ContestsController < ApplicationController authorize :contest @contests = current_user.contests - @title = "Welcome #{current_user.username}!" + @title = I18n.t("contests.index.title", username: current_user.username) end def show authorize @contest - @title = @contest.name + @title = I18n.t("contests.show.title", name: @contest.name) @contestants = @contest.contestants.order(:name) @puzzles = @contest.puzzles.order(:id) set_badges @@ -20,15 +20,12 @@ class ContestsController < ApplicationController def edit authorize @contest - - @title = "Edit contest settings" end def new authorize :contest @contest = Contest.new - @title = "New jigsaw puzzle competition" end def create @@ -65,7 +62,7 @@ class ContestsController < ApplicationController end authorize @contest - @title = @contest.name + @title = I18n.t("contests.scoreboard.title", name: @contest.name) @contestants = @contest.contestants.order(:name) @puzzles = @contest.puzzles.order(:id) render :scoreboard diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb index c105d67..36d73f7 100644 --- a/app/controllers/puzzles_controller.rb +++ b/app/controllers/puzzles_controller.rb @@ -4,15 +4,12 @@ class PuzzlesController < ApplicationController def edit authorize @contest - - @title = "Edit contest puzzle" end def new authorize @contest @puzzle = Puzzle.new - @title = "New contest puzzle" end def create @@ -23,7 +20,6 @@ class PuzzlesController < ApplicationController if @puzzle.save redirect_to contest_path(@contest) else - @title = "New contest puzzle" render :new, status: :unprocessable_entity end end @@ -34,7 +30,6 @@ class PuzzlesController < ApplicationController if @puzzle.update(puzzle_params) redirect_to @contest else - @title = "Edit contest puzzle" render :edit, status: :unprocessable_entity end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index d6d306c..95b8859 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,7 +4,6 @@ class SessionsController < ApplicationController before_action :skip_authorization def new - @title = "Login" end def create diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d85b9ea..0615600 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,14 +4,11 @@ class UsersController < ApplicationController def index authorize :user - @title = "All users" @users = User.all end def edit authorize @user - - @title = "My settings" end def update @@ -33,7 +30,6 @@ class UsersController < ApplicationController def new authorize :user - @title = "New user" @user = User.new() end @@ -44,7 +40,6 @@ class UsersController < ApplicationController if @user.save redirect_to users_path else - @title = "New user" render :new, status: :unprocessable_entity end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 6c349ae..c183c9c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -28,4 +28,39 @@ # enabled: "ON" en: - hello: "Hello world" + completions: + edit: + title: "Edit completion" + new: + title: "New completion" + contests: + edit: + title: "Edit contest settings" + index: + title: "Welcome %{username}!" + new: + title: "New jigsaw puzzle competition" + scoreboard: + title: "%{name}" + show: + title: "%{name}" + contestants: + edit: + title: "Contestant" + new: + title: "New contestant" + puzzles: + edit: + title: "Edit contest puzzle" + new: + title: "New contest puzzle" + sessions: + new: + title: "Login to the Public Scoreboard app" + users: + edit: + title: "My settings" + index: + title: "All users" + new: + title: "New user" diff --git a/spec/features/login_spec.rb b/spec/features/login_spec.rb index 682da7f..88c4a48 100644 --- a/spec/features/login_spec.rb +++ b/spec/features/login_spec.rb @@ -10,7 +10,7 @@ RSpec.feature "Login", type: :feature do fill_in "Password", with: user.password click_button "Sign in" - expect(page).not_to have_content("Login") + expect(page).not_to have_content(I18n.t("sessions.new.title")) end it "should fail to log in the user with an incorrect email address" do @@ -19,7 +19,7 @@ RSpec.feature "Login", type: :feature do fill_in "Password", with: user.password click_button "Sign in" - expect(page).to have_content("Login") + expect(page).to have_content(I18n.t("sessions.new.title")) end it "should fail to log in the user with an incorrect password" do @@ -28,7 +28,7 @@ RSpec.feature "Login", type: :feature do fill_in "Password", with: Faker::Internet.unique.password click_button "Sign in" - expect(page).to have_content("Login") + expect(page).to have_content(I18n.t("sessions.new.title")) end end end diff --git a/spec/features/user_spec.rb b/spec/features/user_spec.rb index 274ed9e..4ebd207 100644 --- a/spec/features/user_spec.rb +++ b/spec/features/user_spec.rb @@ -18,7 +18,7 @@ RSpec.feature "Users", type: :feature do it "should not be able to see the user list" do visit users_path - expect(page).not_to have_content("All users") + expect(page).not_to have_content(I18n.t("users.index.title")) end it "should be able to create a new contest" do @@ -26,16 +26,16 @@ RSpec.feature "Users", type: :feature do click_link "Create a new contest" - expect(page).to have_content("New jigsaw puzzle competition") + expect(page).to have_content(I18n.t("contests.new.title")) end it "should be able to open an existing contest" do visit root_path expect(page).to have_content(contest.name) - find("div.card", text: contest.name).find("a").click() + find("div.card", text: contest.name).find("a").click - expect(page).to have_content("Edit contest") + expect(page).to have_content(I18n.t("contests.show.title", name: contest.name)) end end @@ -56,7 +56,7 @@ RSpec.feature "Users", type: :feature do it "should be able to see the user list" do visit users_path - expect(page).to have_content("All users") + expect(page).to have_content(I18n.t("users.index.title")) expect(page).to have_content(user.username) end end