Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locale selector to email preview #19923

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions railties/lib/rails/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Rails::ApplicationController < ActionController::Base # :nodoc:
self.view_paths = File.expand_path('../templates', __FILE__)
layout 'application'

before_action :set_locale
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to generic ApplicationController? It's an unrelated change to emails, although perhaps useful to discuss in it's own right.


protected

def require_local!
Expand All @@ -13,4 +15,8 @@ def require_local!
def local_request?
Rails.application.config.consider_all_requests_local || request.local?
end

def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
end
30 changes: 26 additions & 4 deletions railties/lib/rails/templates/rails/mailers/email.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
width: 100%;
}
</style>
<script type="text/javascript">
function refreshMailBody() {
part_select = document.querySelectorAll('select#part')[0];
locale_select = document.querySelectorAll('select#locale')[0];

part_param = 'part=' + part_select.options[part_select.selectedIndex].value;
locale_param = 'locale=' + locale_select.options[locale_select.selectedIndex].value;

document.getElementsByName('mailBody')[0].src = '?' + part_param + '&' + locale_param;
}
</script>
</head>

<body>
Expand Down Expand Up @@ -90,16 +101,27 @@

<% 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">View as HTML email</option>
<option <%= request.format == Mime::TEXT ? 'selected' : '' %> value="?part=text%2Fplain">View as plain-text email</option>
<select id="part" onchange="refreshMailBody();">
<option <%= request.format == Mime::HTML ? 'selected' : '' %> value="text%2Fhtml">View as HTML email</option>
<option <%= request.format == Mime::TEXT ? 'selected' : '' %> value="text%2Fplain">View as plain-text email</option>
</select>
</dd>
<% end %>

<% if I18n.available_locales.count > 1 %>
<dt>Locale:</dt>
<dd>
<select id="locale" onchange="refreshMailBody();">
<% I18n.available_locales.each do |locale| %>
<option <%= I18n.locale == locale ? 'selected' : '' %> value="<%= locale %>"><%= locale %></option>
<% end %>
</select>
</dd>
<% end %>
</dl>
</header>

<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>"></iframe>
<iframe seamless name="mailBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>&locale=<%= I18n.locale %>"></iframe>

</body>
</html>