From e35bddf915ea332c0a169556d581d41744b6f0ad Mon Sep 17 00:00:00 2001 From: f Date: Wed, 24 May 2023 10:04:22 -0300 Subject: [PATCH 1/2] fix: no permitir que les usuaries elijan un idioma que no existe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit porque se les rompe el panel después --- app/models/usuarie.rb | 12 ++++++++++++ config/locales/en.yml | 4 ++++ config/locales/es.yml | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/app/models/usuarie.rb b/app/models/usuarie.rb index 2bc7a1b5..42f20c0b 100644 --- a/app/models/usuarie.rb +++ b/app/models/usuarie.rb @@ -10,6 +10,7 @@ class Usuarie < ApplicationRecord validates_uniqueness_of :email validates_with EmailAddress::ActiveRecordValidator, field: :email + validate :locale_available! before_create :lang_from_locale! before_update :remove_confirmation_invitation_inconsistencies! @@ -78,4 +79,15 @@ class Usuarie < ApplicationRecord self.invitation_accepted_at ||= Time.now.utc end end + + # Muestra un error si el idioma no está disponible al cambiar el + # idioma de la cuenta. + # + # @return [nil] + def locale_available! + return if I18n.locale_available? self.lang + + errors.add(:lang, I18n.t('activerecord.errors.models.usuarie.attributes.lang.not_available')) + nil + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5f97a8b9..ab6a9305 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -194,6 +194,10 @@ en: layout_incompatible: error: "Design can't be changed because there are posts with incompatible layouts" help: "Your site has posts with layouts only compatible with the current design. If you change it, the site won't work as you expect. If you're trying out designs, you can delete posts in the following incompatible layouts:: %{layouts}." + usuarie: + attributes: + lang: + not_available: "This language is not yet available, would you help us by translating Sutty into it?" errors: argument_error: 'Argument `%{argument}` must be an instance of %{class}' unknown_locale: 'Unknown %{locale} locale' diff --git a/config/locales/es.yml b/config/locales/es.yml index 9e0b8945..e3c271da 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -194,6 +194,10 @@ es: layout_incompatible: error: 'No se puede cambiar la plantilla porque hay artículos con formatos incompatibles' help: 'En tu sitio hay artículos que solo son compatibles con el diseño actual, si cambias la plantilla el sitio no funcionará como esperas. Si estás probando plantillas, puedes eliminar los artículos en los formatos incompatibles: %{layouts}.' + usuarie: + attributes: + lang: + not_available: "Este idioma todavía no está disponible, ¿nos ayudas a agregarlo y mantenerlo?" errors: argument_error: 'El argumento `%{argument}` debe ser una instancia de %{class}' unknown_locale: 'El idioma %{locale} es desconocido' From 4e002e042bc5b0e5c129ab8cac3df03cf340fefb Mon Sep 17 00:00:00 2001 From: f Date: Wed, 24 May 2023 10:06:11 -0300 Subject: [PATCH 2/2] fix: no permitir idiomas que no existen en el cambio de idioma #13493 --- app/controllers/application_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ee153394..2746ab10 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -59,7 +59,11 @@ class ApplicationController < ActionController::Base # # @return [String,Symbol] def current_locale - session[:locale] = params[:change_locale_to] if params[:change_locale_to].present? + locale = params[:change_locale_to] + + if locale.present? && I18n.locale_available?(locale) + session[:locale] = params[:change_locale_to] + end session[:locale] || current_usuarie&.lang || I18n.locale end