Skip to content

Instantly share code, notes, and snippets.

@ybart
Last active August 29, 2015 14:14
Show Gist options
  • Save ybart/dab3fef2a2cfd2144c5c to your computer and use it in GitHub Desktop.
Save ybart/dab3fef2a2cfd2144c5c to your computer and use it in GitHub Desktop.
Localizing ActionMailer::Preview
class Application < Rails::Application
...
# The easiest way : update the default locale and restart
config.i18n.default_locale = :fr
...
end
class MyMailerPreview < MailerPreview
def notify_error
MyMailer.notify_error(MyModel.last)
end
def notify_error_fr
I18n.with_locale(:fr) do
notify_error
end
end
end
class Rails::MailersController < Rails::ApplicationController
prepend_view_path File.expand_path('../../../../lib/templates', __FILE__)
before_action :set_locale
def set_locale
I18n.locale = params['locale'] || I18n.default_locale
end
end
<% unless @email.attachments.nil? || @email.attachments.empty? %>
<dt>Attachments:</dt>
<dd>
<%= @email.attachments.map { |a| a.respond_to?(:original_filename) ? a.original_filename : a.filename }.inspect %>
</dd>
<% end %>
<% if @email.multipart? %>
<dd>
<select onchange="document.getElementsByName('messageBody')[0].src=this.options[this.selectedIndex].value;">
<option <%= request.format == Mime::HTML ? 'selected' : '' %> value="?part=text%2Fhtml&amp;locale=<%= I18n.locale %>">View as HTML email</option>
<option <%= request.format == Mime::TEXT ? 'selected' : '' %> value="?part=text%2Fplain&amp;locale=<%= I18n.locale %>">View as plain-text email</option>
</select>
</dd>
<% end %>
</dl>
</header>
<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>&amp;locale=<%= I18n.locale %>"></iframe>
class MailerPreview < ActionMailer::Preview
before_action :set_locale
def set_locale
I18n.locale = params['locale'] || I18n.default_locale
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment