-->

Before_validation :set_locale vs I18n.t(text: …, l

2019-08-16 00:11发布

问题:

What is better to you ?

  • precise the locale in all I18n.t(....) messages of the page, or
  • set_locale in a before_validation logic ?

I'm concern about DRY but also performance.

回答1:

You should save your locale in your session and set it by default at your application starting.

I recommend you to read this article : best practices



回答2:

You should use both methods to implement i18n in Rails application.

  1. Set locale in a before_action of controller.(ref: http://guides.rubyonrails.org/i18n.html)
  2. Use I18n.t() to translate text according to the current locale.