From baace1fa5df46694ddd515dc3bbca59b22df8692 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Sat, 25 Jul 2026 03:51:44 +0200 Subject: Extract flashes to i18n and stop dispatching on submit labels --- app/controllers/application_controller.rb | 2 +- app/controllers/assets_controller.rb | 12 +++---- app/controllers/events_controller.rb | 6 ++-- app/controllers/nodes_controller.rb | 45 +++++++++++++------------ app/controllers/occurrences_controller.rb | 4 +-- app/controllers/otp_challenges_controller.rb | 6 ++-- app/controllers/otp_enrollments_controller.rb | 11 +++--- app/controllers/page_translations_controller.rb | 14 ++++---- app/controllers/revisions_controller.rb | 4 +-- app/controllers/sessions_controller.rb | 8 ++--- app/controllers/users_controller.rb | 8 ++--- 11 files changed, 60 insertions(+), 60 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6d46d522..0a7ba109 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -40,7 +40,7 @@ class ApplicationController < ActionController::Base return unless logged_in? return unless current_user.otp_required? && !current_user.otp_enrolled? return if %w[otp_enrollments otp_challenges sessions users].include?(controller_name) - flash[:error] = "Your account requires a second factor -- set it up to continue." + flash[:error] = t("flash.common.otp_required") redirect_to edit_user_path(current_user) end end diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb index 7edd9c05..f1c259e3 100644 --- a/app/controllers/assets_controller.rb +++ b/app/controllers/assets_controller.rb @@ -49,7 +49,7 @@ class AssetsController < ApplicationController respond_to do |format| if @asset.save - flash[:notice] = 'Asset was successfully created.' + flash[:notice] = t("flash.assets.created") NodeAction.record!(:participants => [@asset], :user => current_user, :action => "asset_create", :asset_name => @asset.name, @@ -72,7 +72,7 @@ class AssetsController < ApplicationController respond_to do |format| if @asset.update(asset_params) - flash[:notice] = 'Asset was successfully updated.' + flash[:notice] = t("flash.assets.updated") format.html { redirect_to(@asset) } format.xml { head :ok } else @@ -105,14 +105,14 @@ class AssetsController < ApplicationController :headline => params[:headline].present?) flash[:notice] = if result[:attached].zero? - "Asset saved — it was already attached to “#{node.title}”." + t("flash.assets.already_attached", :title => node.title) else - "Asset was successfully created and attached to “#{node.title}”." + t("flash.assets.attached", :title => node.title) end case result[:headline] - when :set then flash[:notice] += " It is now the page's headline." + when :set then flash[:notice] += " " + t("flash.common.now_headline") when :kept_existing then flash[:headline_kept_path] = node_path(node) - when :not_eligible then flash[:error] = "This asset type cannot be a headline." + when :not_eligible then flash[:error] = t("flash.common.headline_ineligible") end rescue LockedByAnotherUser flash[:locked_by] = node.lock_owner&.login diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index be3f5476..f2c710bd 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -43,7 +43,7 @@ class EventsController < ApplicationController ) if params[:tag_list].present? && params[:auto_tag_source].present? - flash.now[:notice] = "Tag '#{params[:tag_list]}' was pre-filled because this page is tagged '#{params[:auto_tag_source]}'. You can remove it below." + flash.now[:notice] = t("flash.events.tag_prefilled", :tag => params[:tag_list], :source => params[:auto_tag_source]) end respond_to do |format| @@ -65,7 +65,7 @@ class EventsController < ApplicationController respond_to do |format| if @event.save - flash[:notice] = 'Event was successfully created.' + flash[:notice] = t("flash.events.created") format.html { redirect_to(safe_return_to(params[:return_to] || (@event.node ? edit_node_path(@event.node) : edit_event_path(@event)))) } format.xml { render :xml => @event, :status => :created, :location => @event } else @@ -82,7 +82,7 @@ class EventsController < ApplicationController respond_to do |format| if @event.update(event_params) - flash[:notice] = 'Event was successfully updated.' + flash[:notice] = t("flash.events.updated") format.html { redirect_to(safe_return_to(params[:return_to] || events_path)) } format.xml { head :ok } else diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index ce8f0538..50b30881 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb @@ -55,9 +55,9 @@ class NodesController < ApplicationController if params[:asset_id].present? && (asset = Asset.find(params[:asset_id])) result = @node.attach_asset!(asset, :user => current_user, :headline => params[:asset_headline].present?) - flash[:notice] = "Page created with “#{asset.name}” attached." - flash[:notice] += " It is the page's headline." if result[:headline] == :set - flash[:error] = "This asset type cannot be a headline." if result[:headline] == :not_eligible + flash[:notice] = t("flash.nodes.created_with_attachment", :name => asset.name) + flash[:notice] += " " + t("flash.common.now_headline") if result[:headline] == :set + flash[:error] = t("flash.common.headline_ineligible") if result[:headline] == :not_eligible end redirect_to(edit_node_path(@node)) @@ -83,14 +83,12 @@ class NodesController < ApplicationController @page = @node.autosave || @node.draft || @node.head if @node.autosave - flash.now[:notice] = - "This page has unsaved changes from a previous session, shown below. " \ - "Save to keep them, or use \"Discard Autosave\" below to go back to the last saved version." + flash.now[:notice] = t("flash.nodes.autosave_banner") elsif freshly_locked - flash.now[:notice] ||= "Node locked and ready to edit" + flash.now[:notice] ||= t("flash.nodes.locked_ready") end rescue LockedByAnotherUser => e - flash[:error] = e.message + flash[:error] = t("flash.common.locked_by_other") redirect_to(request.referer || node_path(@node)) end @@ -99,7 +97,7 @@ class NodesController < ApplicationController @node.autosave!( page_params.merge(:tag_list => params[:tag_list]), current_user ) @node.save_draft!(current_user) - flash[:notice] = "Draft saved. Publish your changes in the Status section once you're done." + flash[:notice] = t("flash.nodes.draft_saved") flash[:status_path] = node_path(@node) if @node.draft.translated_locales.size > 1 @@ -112,7 +110,7 @@ class NodesController < ApplicationController end end - if params[:commit] == "Save + Unlock + Exit" + if params[:unlock_exit].present? @node.unlock! redirect_to node_path(@node) else @@ -156,13 +154,16 @@ class NodesController < ApplicationController def trash if @node.trash!(current_user) - flash[:notice] = "Page has been moved to the Trash" + flash[:notice] = t("flash.nodes.trashed") redirect_to trashed_nodes_path else - flash[:notice] = "Page is already in the Trash" + flash[:notice] = t("flash.nodes.already_trashed") redirect_to node_path(@node) end - rescue ActiveRecord::RecordInvalid, LockedByAnotherUser => e + rescue LockedByAnotherUser + flash[:error] = t("flash.common.locked_by_other") + redirect_to node_path(@node) + rescue ActiveRecord::RecordInvalid => e flash[:error] = e.message redirect_to node_path(@node) end @@ -170,10 +171,10 @@ class NodesController < ApplicationController def restore_from_trash parent = Node.find(params[:parent_id]) @node.restore_from_trash!(parent, current_user) - flash[:notice] = "Page has been restored from the Trash" + flash[:notice] = t("flash.nodes.restored") redirect_to node_path(@node) rescue ActiveRecord::RecordNotFound - flash[:error] = "Restore target not found" + flash[:error] = t("flash.nodes.restore_target_missing") redirect_to node_path(@node) rescue ActiveRecord::RecordInvalid => e flash[:error] = e.message @@ -182,7 +183,7 @@ class NodesController < ApplicationController def destroy @node.destroy_from_trash!(current_user) - flash[:notice] = "Page has been permanently deleted" + flash[:notice] = t("flash.nodes.deleted") redirect_to trashed_nodes_path rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotDestroyed => e @@ -192,15 +193,15 @@ class NodesController < ApplicationController def publish @node.publish_draft!(current_user) - flash[:notice] = "Draft has been published" + flash[:notice] = t("flash.nodes.published") redirect_to node_path(@node) end def unlock if @node.unlock! - flash[:notice] = "Node unlocked" + flash[:notice] = t("flash.nodes.unlocked") else - flash[:notice] = "Already unlocked" + flash[:notice] = t("flash.nodes.already_unlocked") end redirect_to node_path(@node) @@ -210,9 +211,9 @@ class NodesController < ApplicationController @node = Node.find(params[:id]) if @node.draft @node.draft.ensure_preview_token! - flash[:notice] = "Shareable preview link created - see below." + flash[:notice] = t("flash.nodes.preview_created") else - flash[:notice] = "Create or edit a draft first - shared preview links are only available for pages with an active draft." + flash[:notice] = t("flash.nodes.preview_needs_draft") end redirect_to node_path(@node) end @@ -220,7 +221,7 @@ class NodesController < ApplicationController def revoke_shared_preview @node = Node.find(params[:id]) @node.draft.revoke_preview_token! if @node.draft - flash[:notice] = "Shareable preview link revoked." + flash[:notice] = t("flash.nodes.preview_revoked") redirect_to node_path(@node) end diff --git a/app/controllers/occurrences_controller.rb b/app/controllers/occurrences_controller.rb index 0f30ce38..2e3c2ec1 100644 --- a/app/controllers/occurrences_controller.rb +++ b/app/controllers/occurrences_controller.rb @@ -49,7 +49,7 @@ class OccurrencesController < ApplicationController respond_to do |format| if @occurrence.save - flash[:notice] = 'Occurrence was successfully created.' + flash[:notice] = t("flash.occurrences.created") format.html { redirect_to(@occurrence) } format.xml { render :xml => @occurrence, :status => :created, :location => @occurrence } else @@ -66,7 +66,7 @@ class OccurrencesController < ApplicationController respond_to do |format| if @occurrence.update(occurrence_params) - flash[:notice] = 'Occurrence was successfully updated.' + flash[:notice] = t("flash.occurrences.updated") format.html { redirect_to(@occurrence) } format.xml { head :ok } else diff --git a/app/controllers/otp_challenges_controller.rb b/app/controllers/otp_challenges_controller.rb index e31c36ca..eeaeac20 100644 --- a/app/controllers/otp_challenges_controller.rb +++ b/app/controllers/otp_challenges_controller.rb @@ -19,7 +19,7 @@ class OtpChallengesController < ApplicationController session[:otp_attempts] = session[:otp_attempts].to_i + 1 if session[:otp_attempts] > MAX_ATTEMPTS clear_pending - flash[:error] = "Too many attempts -- log in again." + flash[:error] = t("flash.otp.too_many_attempts") return redirect_to login_path end @@ -28,10 +28,10 @@ class OtpChallengesController < ApplicationController reset_session self.current_user = user session[:logged_in_at] = Time.now.to_i - flash[:notice] = "Logged in successfully" + flash[:notice] = t("flash.common.logged_in") redirect_to safe_return_to(return_to, :default => admin_path) else - flash.now[:error] = "That code did not match." + flash.now[:error] = t("flash.otp.code_mismatch") render :new end end diff --git a/app/controllers/otp_enrollments_controller.rb b/app/controllers/otp_enrollments_controller.rb index 7a31d1e1..54b82214 100644 --- a/app/controllers/otp_enrollments_controller.rb +++ b/app/controllers/otp_enrollments_controller.rb @@ -15,7 +15,7 @@ class OtpEnrollmentsController < ApplicationController # unattended logged-in session cannot be enrolled onto a stranger's phone. def create unless User.authenticate(current_user.login, params[:current_password].to_s) - flash[:error] = "Wrong password." + flash[:error] = t("flash.otp.wrong_password") return redirect_to edit_user_path(current_user) end current_user.begin_otp_enrollment! @@ -25,11 +25,10 @@ class OtpEnrollmentsController < ApplicationController # Confirms with the first generated code. def update if current_user.confirm_otp_enrollment!(params[:code]) - flash[:notice] = "Second factor enabled. The code you just entered is " \ - "spent -- wait for the next one before logging in with it." + flash[:notice] = t("flash.otp.enabled") redirect_to edit_user_path(current_user) else - flash.now[:error] = "That code did not match. Rescan or wait for the next code." + flash.now[:error] = t("flash.otp.code_mismatch_rescan") render :show end end @@ -38,11 +37,11 @@ class OtpEnrollmentsController < ApplicationController def destroy unless User.authenticate(current_user.login, params[:current_password].to_s) && current_user.verify_otp!(params[:code]) - flash[:error] = "Password or code wrong -- second factor unchanged." + flash[:error] = t("flash.otp.wrong_credentials") return redirect_to edit_user_path(current_user) end current_user.disable_otp!(:actor => current_user) - flash[:notice] = "Second factor disabled." + flash[:notice] = t("flash.otp.disabled") redirect_to edit_user_path(current_user) end end diff --git a/app/controllers/page_translations_controller.rb b/app/controllers/page_translations_controller.rb index 38a7c4f9..e5ca57e0 100644 --- a/app/controllers/page_translations_controller.rb +++ b/app/controllers/page_translations_controller.rb @@ -16,14 +16,14 @@ class PageTranslationsController < ApplicationController @page = @node.draft || @node.head @translation = @page.translations.find_by(:locale => @locale) rescue LockedByAnotherUser => e - flash[:error] = e.message + flash[:error] = t("flash.common.locked_by_other") redirect_to node_path(@node) end def update Globalize.with_locale(@locale) { @node.autosave!(translation_params, current_user) } @node.save_draft!(current_user) - flash[:notice] = "#{@locale.upcase} translation saved. Publish the draft to make it live." + flash[:notice] = t("flash.page_translations.saved", :lang => @locale.upcase) if params[:commit] == "Save + Unlock + Exit" @node.unlock! @@ -32,7 +32,7 @@ class PageTranslationsController < ApplicationController redirect_to edit_node_translation_path(@node, @locale) end rescue LockedByAnotherUser => e - flash[:error] = e.message + flash[:error] = t("flash.common.locked_by_other") redirect_to node_path(@node) end @@ -40,7 +40,7 @@ class PageTranslationsController < ApplicationController Globalize.with_locale(@locale) { @node.autosave!(translation_params, current_user) } head :ok rescue LockedByAnotherUser => e - render plain: e.message, status: :locked + render plain: t("flash.common.locked_by_other"), status: :locked rescue ActiveRecord::RecordInvalid => e render plain: e.message, status: :unprocessable_entity rescue StandardError => e @@ -50,12 +50,12 @@ class PageTranslationsController < ApplicationController def destroy base = @node.draft || @node.head unless base && base.translated_locales.include?(@locale) - flash[:error] = "No #{@locale.to_s.upcase} translation exists to remove." + flash[:error] = t("flash.page_translations.none_to_remove", :lang => @locale.to_s.upcase) return redirect_to node_path(@node) end if (base.translated_locales - [@locale]).empty? - flash[:error] = "Can't remove the only remaining translation." + flash[:error] = t("flash.page_translations.last_translation") return redirect_to node_path(@node) end @@ -63,7 +63,7 @@ class PageTranslationsController < ApplicationController draft.translations.where(:locale => @locale).delete_all draft.reload - flash[:notice] = "#{@locale.upcase} translation removed from the draft. Publish to make this permanent." + flash[:notice] = t("flash.page_translations.removed", :lang => @locale.upcase) redirect_to node_path(@node) rescue LockedByAnotherUser => e flash[:error] = e.message diff --git a/app/controllers/revisions_controller.rb b/app/controllers/revisions_controller.rb index c1237a95..9f920b03 100644 --- a/app/controllers/revisions_controller.rb +++ b/app/controllers/revisions_controller.rb @@ -26,7 +26,7 @@ class RevisionsController < ApplicationController @end = @node.resolve_page_reference(params[:end_revision]) if @start.nil? || @end.nil? - flash[:error] = "That comparison is no longer available." + flash[:error] = t("flash.revisions.unavailable") redirect_to(node_path(@node)) and return end @@ -50,7 +50,7 @@ class RevisionsController < ApplicationController def restore page = Page.find(params[:id]) page.node.restore_revision! page.revision, current_user - flash[:notice] = "Revision #{page.revision} restored" + flash[:notice] = t("flash.revisions.restored", :rev => page.revision) redirect_to node_path(page.node) end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 49d33810..bb63c51e 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -32,10 +32,10 @@ class SessionsController < ApplicationController session[:logged_in_at] = Time.now.to_i if user.otp_required? - flash[:error] = "Your account requires a second factor -- set it up now." + flash[:error] = t("flash.sessions.otp_setup_now") redirect_to edit_user_path(user) else - flash[:notice] = "Logged in successfully" + flash[:notice] = t("flash.common.logged_in") redirect_to safe_return_to(return_to, :default => admin_path) end end @@ -48,14 +48,14 @@ class SessionsController < ApplicationController def destroy logout_killing_session! - flash[:notice] = "You have been logged out." + flash[:notice] = t("flash.sessions.logged_out") redirect_back_or_default('/login') end protected # Track failed login attempts def note_failed_signin - flash[:error] = "login not successful" + flash[:error] = t("flash.sessions.failed") logger.warn "Failed login for '#{params[:login]}'" \ "from #{request.remote_ip} at #{Time.now.utc}" end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 08541b0c..12531d86 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -22,7 +22,7 @@ class UsersController < ApplicationController @user = User.new user_params if @user.save - flash[:notice] = "User created #{@user.login}" + flash[:notice] = t("flash.users.created", :login => @user.login) redirect_to user_path(@user) else render :new @@ -37,7 +37,7 @@ class UsersController < ApplicationController permitted.delete(:admin) unless current_user.is_admin? if @user.update(permitted) - flash[:notice] = "Updated user #{@user.login}" + flash[:notice] = t("flash.users.updated", :login => @user.login) redirect_to user_path(@user) else render :edit @@ -55,7 +55,7 @@ class UsersController < ApplicationController def reset_otp return deny_user_access unless current_user.admin? @user.disable_otp!(:actor => current_user) - flash[:notice] = "Second factor reset for #{@user.login}" + flash[:notice] = t("flash.users.otp_reset", :login => @user.login) redirect_to edit_user_path(@user) end @@ -79,7 +79,7 @@ class UsersController < ApplicationController end def deny_user_access - flash[:notice] = "Sorry, you need to be an admin for this action" + flash[:notice] = t("flash.common.admin_required") redirect_to users_path end end -- cgit v1.3