Ruby on Rails:插入不同语言(区域设置)的部分

发布于 2024-12-12 21:55:57 字数 706 浏览 1 评论 0原文

有一个视图在我的多语言应用程序中调用部分:

  <%= render :partial => 'shared/order', :object => @order %>

默认语言是俄语,并且 shared/_order.html.erb 使用 I18n.t

<%= t('.order_type') %>: <b><%= t("order_types.#{order.order_type}") %></b>
<%= t '.date_and_time' %>: <b><%= l(order.date, :format => :long) %>

我想再次渲染相同的部分但在不更改 I18n.locale 变量的情况下使用英语。通过写这样的东西:

  <%= render :partial => 'shared/order', :object => @order %>
  <%= render :partial => 'shared/order', :object => @order, :locale => 'en' %>

实现这种行为的最佳实践是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜还是长夜 2024-12-19 21:55:57

您始终可以使用类似的方法在特定区域设置中运行代码块:

<%= render :partial => 'shared/order', :object => @order %>
<%- I18n.with_locale('en') do %>
  <%= render :partial => 'shared/order', :object => @order %>
<%- end %>

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 %>
原来是傀儡 2024-12-19 21:55:57

嗯嗯。很难回答的问题。我会做以下事情:

<%= render :partial => 'shared/order', :object => @order %>
<%= render :partial => 'shared/order', :object => @order, :locals => {:locale => 'en'} %>

然后在局部视图中:

<% before_locale = I18n.locale %>
<% locale ||= before_locale %>
<% I18n.locale = locale %>

..... do things here ....

<% # return back to original locale %>
<% I18n.locale = before_locale %>

我真的不知道这是否是最好的。我刚刚想到作为解决你的问题的第一个解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文