Translations Not Working For validation messages from ValidationMessages module
Please complete the following fields as applicable:
What version of the DMPRoadmap code are you running? (e.g. v2.2.0)
- v4.1.1
Expected behaviour:
- Validation messages should render in the same language that is selected within the app.
Actual behaviour:
- Validation messages where a
message:argument is provided (e.g.validates :text, presence: { message: QUESTION_TEXT_PRESENCE_MESSAGE }only appear to render in the default language. - When an attribute name is included in a validation message, its name is always in the default language.
Steps to reproduce:
- Select a non-default language
- Perform any action that triggers a validation messages where a
message:argument is provided - Observe the rendered validation message.
Screenshots of some rendered validation messages when the 'Français (CA)' language is selected:
Translations of attribute names can be enabled in config/locales/en.yml 4.5 Translations for Active Record Models.
Our validation message translations could be handled in config/locales/en.yml as well: 4.5.1 Error Message Scopes
I'm not sure if this is what you need, but I was able to change the language for both alerts and the platform by changing the following lines of code:
In /roadmap/config/initializers/translation.rb:
DEFAULT_LOCALE = 'es'
In /roadmap/config/initializers/_dmproadmap.rb:
config.x.locales.default = 'es'
Hi @aalvan, thanks for the reply! I'm still encountering some trouble on my end. Here's a bit more context:
Loading development environment (Rails 6.1.7.7)
3.0.5 :001 > I18n.locale = :'fr-FR'
=> :"fr-FR"
3.0.5 :002 > _("can't be blank")
=> "ne peut être vide"
3.0.5 :003 > user = User.first.dup
User Load (1.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
=> #<User id: nil, firstname: "DMP", surname: "Administrator", email: "[email protected]", created_at: nil, updated_at: nil, other_organisati...
3.0.5 :004 > user.firstname = ''
=> ""
3.0.5 :005 > user.surname = ''
=> ""
3.0.5 :006 > user.org = nil
=> nil
3.0.5 :007 > user.valid?
User Exists? (2.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2 [["email", "[email protected]"], ["LIMIT", 1]]
Language Load (1.0ms) SELECT "languages".* FROM "languages" WHERE "languages"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
=> false
3.0.5 :008 > user.errors.full_messages
=>
["Courriel: n'est pas disponible",
"Password Ce champ ne peut être vide",
"Org doit exister",
"Firstname can't be blank",
"Surname can't be blank",
"Org can't be blank"]
3.0.5 :009 >
_("can't be blank") translates successfully. But the same isn't the case for the validation messages. Also, here's a screenshot of what is rendered within the app.
In particular, it's the validation messages within app/models/concerns/validation_messages.rb that don't seem to be translating.
Hi @aalvan, I also tried implementing your changes using 'fr-Fr' for the language. The validation messages successfully translate to French, but now they fail to translate to English. So the issue must be with when switching to non-default app languages.