There is a view which is calling a partial in my multilingual application:
<%= render :partial => 'shared/order', :object => @order %>
the default language is Russian and shared/_order.html.erb uses I18n.t
<%= t('.order_type') %>: <b><%= t("order_types.#{order.order_type}") %></b>
<%= t '.date_and_time' %>: <b><%= l(order.date, :format => :long) %>
And I want to render just the same partial one more time but in English without changing the I18n.locale variable. By writing something like this:
<%= render :partial => 'shared/order', :object => @order %>
<%= render :partial => 'shared/order', :object => @order, :locale => 'en' %>
What is the best practices to achieve such behaviour?
You can always use something like this to run a code block in a certain locale:
<%= render :partial => 'shared/order', :object => @order %>
<%- I18n.with_locale('en') do %>
<%= render :partial => 'shared/order', :object => @order %>
<%- end %>
Hmmmmm. Difficult question. I would do the following:
<%= render :partial => 'shared/order', :object => @order %>
<%= render :partial => 'shared/order', :object => @order, :locals => {:locale => 'en'} %>
and then in the partial view:
<% before_locale = I18n.locale %>
<% locale ||= before_locale %>
<% I18n.locale = locale %>
..... do things here ....
<% # return back to original locale %>
<% I18n.locale = before_locale %>
I really do not know whether this is the best. Just crossed my mind as first solution to your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With