将i18n gem与部分模板文件一起使用时出现问题 [英] Trouble on using the i18n gem with partial template files
问题描述
我正在使用Ruby on Rails 3.1,我想知道如何正确处理与部分模板文件有关的国际化.那就是...
I am using Ruby on Rails 3.1 and I would like to know how to correctly handle internationalization related to partial template files. That is, ...
...在我的app/views/users/flag.html.erb
文件中:
... in my app/views/users/flag.html.erb
file I have:
<%= t('.test_key1') %>
<%= render :partial => "/users/flag_form" %>
...在我的app/views/users/_flag_form.html.erb
文件中:
... in my app/views/users/_flag_form.html.erb
file I have:
<%= t('.test_key2') %>
如果在我的config/locales/views/users/en.yml
文件中( note :我正在按照
If in my config/locales/views/users/en.yml
file (note: I am organizing files as stated in the official RoR guide) I use
en:
users:
flag:
test_key1: Test 1 text
test_key2: Test 2 text
Test 1 text
显示在主"模板中(app/views/users/flag.html.erb
),但Test 2 text
不适用于部分模板(app/views/users/_flag_form.html.erb
). 我该如何解决此问题,以便正确显示Test 2 text
?
the Test 1 text
is displayed in the "main" template (app/views/users/flag.html.erb
) but the Test 2 text
isn't for the partial template (app/views/users/_flag_form.html.erb
). How could\should I solve this issue so to properly display the Test 2 text
?
推荐答案
一种方法是使用范围,而不是使用句号延迟加载". 这样的事情应该起作用:
One way would be to using scope, instead of "lazy loading" using the full stop. Something like this should work:
I18n.t :test_key2, :scope => 'users.flag'
或使用:
I18n.t "users.flag.test_key2"
最后,您甚至可以像在
<%= render :partial => "/users/flag_form", :locals => { :test_key => t('.test_key1') } %>
您还应该在此网站上查看附录部分,因为它可能列出了我所缺少的内容: http://www.unixgods.org/~tilo/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
You should also checkout the appendix section on this website as it might be listing something that I am missing: http://www.unixgods.org/~tilo/Rails/where_is_Rails_trying_to_lookup_L10N_strings.html
这篇关于将i18n gem与部分模板文件一起使用时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!