How do I set a fallback locale in Rails 5.2?
Right now it prints ''(blank) for attribute names if not found in locale. I would like it to fallback to :en when that happens.
Rails 5 specific?
# config/application.rb
config.load_defaults 5.0
config.i18n.default_locale = :nb
run specs and get:
#<ActiveRecord::RecordInvalid: Det oppstod en feil: er allerede i bruk>
see the missing part here:
#<ActiveRecord::RecordInvalid: Det oppstod en feil: [MISSING RECORD NAME] er allerede i bruk>
# switch to
# config.i18n.default_locale = :en
#<ActiveRecord::RecordInvalid: Validation failed: Report date has already been taken>
UPDATE:
@Nate got the fallback config correct in his answer below (https://mcmap.net/q/1312326/-rails-5-i18n-default_locale-and-fallback-locale)
That wasn't the issue though. The problem, I learned, was in my nb.yml
.
- Experiment 1. Deleting the entire content of nb.yml, leaving just hello world. Works: It prints the attribute (associated record) name.
- Experiment 2.
Replacing our nb.yml with the official Rails nb.yml: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml : Works. I get the helpful norwegian-english message
Det oppstod feil: Company må eksistere
. Not pretty, but helpful and what I wanted! - Experiment 3. Proof. The nb.yml reproduces the problem.
Two approaches
# A: SHOW attribute name
nb:
errors:
format: "%{attribute} %{message}"
# B: HIDE attribute name
nb:
errors:
format: "%{message}"
Which leads to another problem.
Approach B: HIDE makes you blind serverside. the specs, logs and console will show Validation Failed: can't be blank
but it looks good in UI.
I posted another Q: Simpleform errors without attribute name, but using attribute name in logs, console, specs