首页 文章

Ruby on Rails I18n邮件主题翻译在自定义目录中

提问于
浏览
-1

我正在使用I18n gem进行翻译 . 我将所有翻译都放在一个文件中(en.yml,es.yml ..) . 但对于电子邮件,我没有翻译电子邮件正文中的每个句子,而是为每个区域设置单独的电子邮件视图 . 所以我的电子邮件视图文件结构如下:

- views
    - user_mailer
      - notify_activation.en.html.erb
      - notify_activation.de.html.erb
      - notify_activation.es.html.erb

我的用户邮件是这样的:

class UserMailer < ApplicationMailer
  def notify_activation user
    @user = user
    mail(to: @user.email, subject: t('mailer.user.activation.subject'))
  end
end

现在我想将邮件主题翻译放在语言环境的自定义目录中 . 所以语言环境结构如下所示:

- config
  - locales
    - en
      - user_mailer.yml
      - *other mailers*.yml
    - de
      - user_mailer.yml
      - *other mailers*.yml
    - es
      - user_mailer.yml
      - *other mailers*.yml
    - devise.en.yml
    - en.yml
    - es.yml
    - de.yml

我搜索了一个解决方案,但一切都是徒劳的 . 我遇到过建议我在application.rb中更改i18n.load_path的解决方案,但这会改变所有转换的路径,我只想更改邮件主题的加载路径 . 我希望有类似的东西:

def load_path
  "#{I18n.locale}/#{self.class_name}"
end

在应用程序mailer.rb中 . 任何解决方案或建议将受到高度赞赏 .

P.S我对rails非常陌生,所以不要介意彻底 .

2 回答

  • 0

    Rails自动完成(至少Rails 4) . 默认config / locales / en.yml具有以下注释:

    config / locales目录中的文件用于国际化,并由Rails自动加载 . 如果要使用英语以外的语言环境,请在此目录中添加必要的文件 .

    因此,您可以根据需要在config dir中构建文件 .

  • 0

    如Rails的I18n文档中所述:您可以更改 config/initializers/application.rb 中的 load_path

    I18n.load_path += Dir[Rails.root.join('config', 'locales', 'en', '*.yml')] 
    I18n.load_path += Dir[Rails.root.join('config', 'locales', 'es', '*.yml')] 
    I18n.load_path += Dir[Rails.root.join('config', 'locales', 'de', '*.yml')]
    

相关问题