Pie*_*ois 4 ruby-on-rails decorator internationalization ruby-on-rails-3
我使用Draper gem来装饰我的模型.在这里,我有非常经典的设置:
# app/decorators/subject_decorator.rb
class SubjectDecorator < ApplicationDecorator
decorates :subject
def edit_link
h.link_to(h.t('.edit'), '#')
end
end
Run Code Online (Sandbox Code Playgroud)
我使用i18n进行国际化.但是当我运行这个时,我得到:
Cannot use t(".edit") shortcut because path is not available
Run Code Online (Sandbox Code Playgroud)
所以我想知道以前是否有人这样做过?它应该很直接.
问题是你不能利用装饰器中的延迟查找,因为它们没有任何上下文来确定视图文件级别(索引,显示,编辑等).开箱即用你只需要拼出整件事t('subjects.show.edit')或者其他什么.
这就是我为了让它有点适合我而做的事情.
class ApplicationDecorator < Draper::Base
def translate(key, options={})
if key.to_s[0] == '.'
key = model_class.to_s.downcase.pluralize + key
end
I18n.translate(key, options)
end
alias :t :translate
end
Run Code Online (Sandbox Code Playgroud)
这不会给你完整的subjects.show.edit参考,你会得到subjects.edit但是对我来说似乎总比没有好.
| 归档时间: |
|
| 查看次数: |
2675 次 |
| 最近记录: |