Trouble on using the i18n gem with partial template files

流过昼夜 提交于 2019-12-04 02:05:14

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'

or use:

I18n.t "users.flag.test_key2"

Lastly, you could even pass it to the partial as in

<%= render :partial => "/users/flag_form", :locals => { :test_key => t('.test_key1') } %>

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

config/locales/en.yml

en:
  users:
    flag:
      test_key1: Test 1 text
    flag_form:
      test_key2: Test 2 text

app/views/users/flag.html.erb

<%= t('.test_key1') %>
<%= render :partial => "/users/flag_form" %>

app/views/users/_flag_form.html.erb

<%= t('.test_key2') %>

NB:

  1. Rails path to the view must match YAML path to the symbol. You need to create an entry at YAML file that matches the name of the view. Omit the trailing underscore since it's a partial.
  2. Read more about lazy lookups

I wrote this. What do you think about it?

def translate_for_partials key, *args
  I18n.t("#{params[:controller].gsub('/', '.')}.#{params[:action]}.#{key}", *args)
end

Is that bad to make such a method ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!