[英]Different text i18n in Rails
在我的rails应用程序中,我有一个部分( file1/_partial.html.erb
)必须以两个格式file2/form1.html.erb
和file3/_partial.html.erb
,我想知道什么是最好的解决方案i18n部分文字
实际上我正在做的是
<%= (t :to_ranslate, :scope => 'file2.form1' ) %>
file2:
form1:
to_ranslate: "translated"
这是有效的,但我想知道是否有一个解决方案,我可以根据模板进行不同的翻译,所以对于相同的部分我将根据它的呈现位置有不同的文本
如果您在不同的模板中包含不需要不同的翻译,您可以使用4.1.4中所述的“Lazy Lookup”: http : //guides.rubyonrails.org/i18n.html#looking-up-translations
它也适用于渲染部分,例如:
in file1/_partial.html.erb
<%= (t '.to_translate') %>
in the translations.yml:
en:
file1:
_partial:
to_translate: "translated"
根据您渲染的位置使用不同的翻译,如此(未经测试但原则应该有效):
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"
这样做的好处是,当您不指定translation_scope(回退到默认值)时以及传递它时它仍然有效。 这就是我更喜欢使用范围的原因:'xx'和你一样。 并且整个界面看起来有点像在视图中具有相同结构的继承。
以更抽象和复杂的方式,也可以使用回退,但在这里的示例中,这将是过度的。 在较大的项目中,实现这样的事情可能是有意义的。
据我所知,rails默认是国际化的开箱即用,只需按正常方式放置文本,它应该没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.