在演示者文件中对 i18n 使用相对根
Use relative root for i18n in presenter files
使用像 app/presenters/foo.rb
这样的文件,我希望能够有一个 i18n 密钥 foo.whatever
并在 foo.rb
中引用它作为 I18n.t('.whatever')
,在某种程度上这类似于用视图来做。
这可能吗?我仔细阅读了 Rails 上的 i18n 指南,并在互联网 ("add relative roots to i18n") 上进行了非常彻底的搜索,但无济于事。
foo
是 class 还是模块?你可以让你所有的演示者扩展一个基本模块,比如:
def t(key)
scope = "presenters.#{self.class.to_s.underscore.gsub('/', '.')}"
I18n.t(key, scope: scope, default: I18n.t(key))
end
编辑:更改为正确的Rails语法并使用模块命名空间
使用像 app/presenters/foo.rb
这样的文件,我希望能够有一个 i18n 密钥 foo.whatever
并在 foo.rb
中引用它作为 I18n.t('.whatever')
,在某种程度上这类似于用视图来做。
这可能吗?我仔细阅读了 Rails 上的 i18n 指南,并在互联网 ("add relative roots to i18n") 上进行了非常彻底的搜索,但无济于事。
foo
是 class 还是模块?你可以让你所有的演示者扩展一个基本模块,比如:
def t(key)
scope = "presenters.#{self.class.to_s.underscore.gsub('/', '.')}"
I18n.t(key, scope: scope, default: I18n.t(key))
end
编辑:更改为正确的Rails语法并使用模块命名空间