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



我在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

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

除了在同一文件中手动合并这些键之外,还有什么解决方案吗?

您不需要将两个文件限制在一起,

您可以使用config/locates文件夹中的devise.es.yml进行特定的翻译。请参见此处:https://github.com/plataformatec/devise/wiki/I18n

同时在en.yml 中保存您的其他消息

希望它能有所帮助!

最新更新