Rails i18n:合并来自多个来源的翻译

时间:2015-12-16 22:29:12

标签: ruby-on-rails ruby-on-rails-3 devise

我在使用Rails I18n时遇到了一些问题,更具体地说是使用了errors.messages中的键。

问题在于Rails本身定义了一些翻译( https://github.com/svenfuchs/rails-i18n/blob/a7d33d1b319bdbaa1da64ced0aa0d280891a3a09/rails/locale/en.yml#L101)。示例:

  errors:
    messages:
      accepted: must be accepted
      blank: can't be blank
      present: must be blank
      confirmation: doesn't match %{attribute}
      empty: can't be empty
    ...

但Devise还在errors.messages(https://github.com/tigrish/devise-i18n/blob/ab598c6fdd8a029c9853fcf22788c27b828fae31/rails/locales/en.yml#L50)中定义了一些翻译。例如:

  errors:
    messages:
      already_confirmed: was already confirmed, please try signing in
      confirmation_period_expired: needs to be confirmed within %{period}, please request a new one
      expired: has expired, please request a new one
      not_found: not found
      not_locked: was not locked

如果在Rails翻译文件之前加载了Devise翻译文件,它将被覆盖,反之亦然。

除了在同一个文件中手动合并这些密钥外,还有解决方法吗?

1 个答案:

答案 0 :(得分:0)

您无需同时处理这两个文件,

您可以使用config / locales文件夹中的devise.es.yml进行设计特定翻译。看这里: https://github.com/plataformatec/devise/wiki/I18n

在en.yml

中包含其他消息

希望它有所帮助!