Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5337

I18n message for UniquenessValidator "taken" error is not compatible with Rails I18n

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 8.0.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Minor Change

      UniquenessValidator does not work properly with I18n; the error message is always "stuck" in English. (A non-English user of my app reported this.)

      For all validation error message types EXCEPT uniqueness, adding Rails I18n gem will translate them automatically into the correct locale. Uniqueness is broken in Mongoid and will be stuck in English. (It works correctly in ActiveRecord.)

      The root cause is as follows:

      • When the UniquenessValidator fails, it sets an I18n message "taken"
      • Both the Rails I18n gem AND ActiveRecord put the "taken" message under errors.messages.taken (NOT active_record.errors.messages.taken)
      • The Rails I18n gem looks for message translations first under {{{} {i18n_scope}

        .errors.messages.taken{}}}, THEN under errors.messages.taken. Note that i18n_scope for Mongoid is :mongoid (set in Mongoid::Document)

      • Mongoid puts the translation for taken under mongoid.errors.messages.taken, and because the key exists there, it prevents Rails I18n from falling back correctly as AR does
      • To fix this, we simply need to move the translation in the Mongoid lib to errors.messages.taken, which is what AR / Rails I18n do, as per links above.

      Note that Rails I18n moved the location of the key in 2011: https://github.com/svenfuchs/rails-i18n/blob/master/CHANGELOG.md?plain=1#L579

      I've raised a PR here to fix the issue: https://github.com/mongodb/mongoid/pull/5262. This PR also updates the message to use the same text as latest AR / Rails I18n.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: