Ruby on rails Rails中的不同文本i18n

Ruby on rails Rails中的不同文本i18n,ruby-on-rails,ruby,ruby-on-rails-4,internationalization,Ruby On Rails,Ruby,Ruby On Rails 4,Internationalization,在我的rails应用程序中,我有一个分部(file1/\u partial.html.erb),它必须以两种形式呈现file2/form1.html.erb和file3/\u partial.html.erb,我想知道分部中的文本的最佳解决方案是什么 事实上我正在做的是 <%= (t :to_ranslate, :scope => 'file2.form1' ) %> file2: form1: to_ranslate: "translated" '

在我的rails应用程序中,我有一个分部(
file1/\u partial.html.erb
),它必须以两种形式呈现
file2/form1.html.erb
file3/\u partial.html.erb
,我想知道分部中的文本的最佳解决方案是什么

事实上我正在做的是

<%= (t :to_ranslate, :scope => 'file2.form1' ) %>

  file2:
    form1:
      to_ranslate: "translated"
'file2.form1')%>
文件2:
表格1:
翻译:“翻译”

这是可行的,但我想知道是否有一个解决方案,我可以根据模板进行不同的翻译,因此对于同一部分,我将根据其呈现的位置进行不同的文本

如果您不需要在不同的模板中包含不同的翻译,您可以使用“延迟查找”如4.1.4所述:

它也适用于渲染部分,例如:

in file1/_partial.html.erb
<%= (t '.to_translate') %>

in the translations.yml:
en:
  file1:
    _partial:
      to_translate: "translated"
文件1/_partial.html.erb中的

在translations.yml中:
嗯:
文件1:
_部分:
翻译:“翻译”
用于根据渲染的来源使用不同的翻译,如(未测试,但原则应可行):

文件1/_partial.html.erb中的


据我所知,rails默认是开箱即用的国际化 只需像平常一样放置文本,它应该可以正常工作


参考资料:

如果我需要根据渲染位置对同一部分进行不同的翻译,在这种情况下,我会在渲染部分时传入一个范围,我会立即更新我的答案。我不明白为什么我必须以部分形式渲染部分形式
in file1/_partial.html.erb
<%= (t "#{translation_scope.to_s}.to_translate') %>

in file2/form1.html.erb
<%= render '/file1/partial', translation_scope: 'file2._partial' %>

in file3/_partial.html.erb
<%= render '/file1/partial', translation_scope: 'file3._partial' %>

in the translations.yml
en:
  file1:
    _partial:
      to_translate: "translated"
  file2:
    _partial:
      to_translate: "translated"
  file2:
    _partial:
      to_translate: "translated"