【发布时间】:2014-04-23 10:44:11
【问题描述】:
我知道 i18n 语言环境文件中有一些预设结构,以便 Rails 自动提取值。例如,如果您想为新记录设置默认提交按钮文本:
# /config/locales/en.yml
en:
helpers:
submit:
create: "Create %{model}"
user:
create: "Sign Up"
使用此集合,在视图中将产生以下结果:
# /app/views/things/new.html.erb
<%= f.submit %> #=> Renders a submit button reading "Create Thing"
# /app/views/users/new.html.erb
<%= f.submit %> #=> Renders a submit button reading "Sign Up"
所以 Rails 使用预设层次结构来获取不同模型的提交按钮文本。 (即,当使用f.submit 时,您不必告诉它要获取哪个 i18n 文本。)我一直在尝试找到一种方法来使用快速通知和警报来做到这一点。是否有类似的预设结构来指定默认的 flash 消息?
我知道您可以指定自己的任意结构,如下所示:
# /config/locales/en.yml
en:
controllers:
user_accounts:
create:
flash:
notice: "User account was successfully created."
# /app/controllers/users_controller.rb
def create
...
redirect_to root_url, notice: t('controllers.user_accounts.create.flash.notice')
...
end
但是每次都指定notice: t('controllers.user_accounts.create.flash.notice') 很繁琐。有没有办法做到这一点,以便控制器“只知道”何时获取和显示语言环境文件中指定的适当闪存消息?如果是这样,这些的默认 YAML 结构是什么?
【问题讨论】:
标签: ruby-on-rails internationalization locale flash-message