Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: inserting a partial in different language (locale)

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?

like image 709
installero Avatar asked Oct 30 '11 21:10

installero


2 Answers

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 %>
like image 147
Sebastian Oelke Avatar answered Nov 09 '22 15:11

Sebastian Oelke


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.

like image 32
p.matsinopoulos Avatar answered Nov 09 '22 14:11

p.matsinopoulos