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 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 8257fed6..a110947e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -191,6 +191,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 ec8e1afa..6eb35a6d 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -191,6 +191,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'