-1

I am using I18n gem for the translation. I have all my translation in a single file(en.yml, es.yml.. ). But for emails, instead of translating each sentence in the email body I have a separate email view for each locale. So my email view files are structured like:

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

and my user mailer is like:

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

Now I want to place the mailer subject translation in a custom directory in the locale. So the locale structure will look like:

- 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

I searched for a solution but all in vain. I have come across solutions suggesting me to change the i18n.load_path in application.rb but that would change the path for all the translation and I only want to change load path for mailer subject. I was hoping for something like:

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

in the application mailer.rb. Any solutions or suggestions will be highly appreciated.

P.S I am quite new to rails so don't mind being thorough.

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
sonm10
  • 53
  • 2
  • 6

2 Answers2

0

Rails do it automatically (at least Rails 4+). Default config/locales/en.yml has the following comment:

Files in the config/locales directory are used for internationalization
and are automatically loaded by Rails. If you want to use locales other
than English, add the necessary files in this directory.

So you can structure files in config dir as you wish.

user3309314
  • 2,453
  • 2
  • 17
  • 30
0

As stated in the Rails' I18n documentation: You can change the load_path in your config/initializers/application.rb

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')]