在rails 5.2上,我想用:en和:pt语言填充我的db,我尝试:
Product.create!(
process: t('washed'),
category: coffee)然后给出我的en.yml和pt.yml文件中的翻译。我收到了一条错误消息:
NoMethodError: undefined method `t' for main:Object有什么想法吗?
-编辑
我以为我找到了解决方案,但我不能让它与嵌套属性一起工作:
在我的seed.rb:
Product.create!(
name:'Cerro de Jesus farm',
process: I18n.t('process'),
cupping_notes: I18n.t('cup_notes.nica_cp'),
category: coffee)在我的pt.yml:
pt:
process: "lavado"
cup_notes:
nica_cp: 'café, limão'
columb_cp: 'limão'在我看来:
<p><%= t('cup_notes', cupping_notes:@product.cupping_notes)%></p>该视图显示整个散列:拔罐注释:{:nica_cp=>“café,lim o”,:columb_cp=>“lim o”}
知道我错过了什么吗?谢谢!!
发布于 2020-10-08 14:06:15
t是I18n.t的缩写,但只在视图和助手中工作。在控制器或模型中使用I18n.t
https://guides.rubyonrails.org/i18n.html#the-public-i18n-api
如果您希望将此部分作为视图的一部分,则需要执行以下操作:
pt.yml
pt:
process: "lavado"
cup_notes:
nica_cp: 'café, limão'
columb_cp: 'limão'在你看来:
<p><%= t("cup_notes.#{@product.cupping_notes}")%></p>供参考
你试图使用:
<p><%= t('cup_notes', cupping_notes:@product.cupping_notes)%></p>没有工作,因为第二个参数用于将变量传递给特定的转换。
例如,如果我的翻译中有这样的内容:
pt:
number_of_lemons: "%{number} limões"
``
Then I can do this in my view:
有关更多信息,请参见翻译变量。
https://stackoverflow.com/questions/64263880
复制相似问题