roadmap icon indicating copy to clipboard operation
roadmap copied to clipboard

Translations Not Working For validation messages from ValidationMessages module

Open aaronskiba opened this issue 2 years ago • 4 comments

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:

  1. Select a non-default language
  2. Perform any action that triggers a validation messages where a message: argument is provided
  3. Observe the rendered validation message.

Screenshots of some rendered validation messages when the 'Français (CA)' language is selected:

Screenshot 2023-12-05 at 11 18 08 AM Screenshot 2023-12-05 at 11 19 53 AM Screenshot 2023-12-05 at 11 27 11 AM

aaronskiba avatar Dec 05 '23 20:12 aaronskiba

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

aaronskiba avatar Dec 05 '23 20:12 aaronskiba

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'

aalvan avatar May 30 '24 13:05 aalvan

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. Screenshot from 2024-05-30 08-40-06

In particular, it's the validation messages within app/models/concerns/validation_messages.rb that don't seem to be translating.

aaronskiba avatar May 30 '24 14:05 aaronskiba

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. Screenshot from 2024-05-30 08-52-07 Screenshot from 2024-05-30 09-00-02

aaronskiba avatar May 30 '24 15:05 aaronskiba