我在创建通知系统的过程中。系统应该通知用户模型中的一些更改,即这是我的.yml
文件
ru:
notifications:
task: "New task named %{notifiable.task_name}"
order: "New order with price %{notifiable.price}"
您现在了解我有一个关联User has_many Notification
和Notification has_one(polymorphic) Notifiable
。因此,您了解Order
没有归属task_name
而Task
没有归属price
。如何将哈希传递给i18n或以另一种方式实现此逻辑?
答案 0 :(得分:0)
哦,明白了!
请考虑以下代码:
# making a hash
def serialized_message_object
attr_hash = {}
@user.attribute_names.each { |attr_name| attr_hash[('user_' + attr_name).to_sym] = @user[attr_name] }
@user.attribute_names.each { |attr_name| attr_hash[('notifiable_' + attr_name).to_sym] = @notifiable[attr_name] }
attr_hash
end
# call I18n
I18n.t('notifications.' + message_type, serialized_message_object)
# i18n
ru:
notifications:
task: "New task named %{notifiable_task_name}"
order: "New order with price %{notifiable_price}"