Skip to content

Commit 8c5e29d

Browse files
Ensure auth keys at the start of the i18n msg are properly cased
Otherwise if we humanized the whole string, it could cause us to change the output of strings with periods and maybe other side-effects, since we're changing the whole string from i18n. This is safer as it only changes the first char of the translated message, and only if it is a match with the first translated auth key, so we can more safely humanize & downcase all auth keys to interpolate in the message whenever needed.
1 parent 89942c2 commit 8c5e29d

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/devise/failure_app.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ def i18n_message(default = nil)
111111
options[:scope] = "devise.failure"
112112
options[:default] = [message]
113113
auth_keys = scope_class.authentication_keys
114-
keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key| scope_class.human_attribute_name(key).downcase }
115-
options[:authentication_keys] = keys.join(I18n.t(:"support.array.words_connector"))
114+
human_keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key|
115+
scope_class.human_attribute_name(key).downcase
116+
}
117+
options[:authentication_keys] = human_keys.join(I18n.t(:"support.array.words_connector"))
116118
options = i18n_options(options)
117-
translated_message = I18n.t(:"#{scope}.#{message}", **options)
118-
# only call `#humanize` when the message is `:invalid` to ensure the original format
119-
# of other messages - like `:does_not_exist` - is kept.
120-
message == :invalid ? translated_message.humanize : translated_message
119+
120+
I18n.t(:"#{scope}.#{message}", **options).then { |msg|
121+
# Ensure that auth keys at the start of the translated string are properly cased.
122+
msg.start_with?(human_keys.first) ? msg.upcase_first : msg
123+
}
121124
else
122125
message.to_s
123126
end

0 commit comments

Comments
 (0)