在Rails中,如何在i18n语言环境文件中指定默认的Flash消息 [英] In Rails, how to specify default flash messages in i18n locale file

查看:178
本文介绍了在Rails中,如何在i18n语言环境文件中指定默认的Flash消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在i18n语言环境文件中有一些预设结构,因此Rails会自动提取值.例如,如果要为新记录设置默认的提交"按钮文本:

I know there are some preset structures in i18n locale file so that Rails pulls values automatically. For example, if you want to set the default submit button text for new records:

# /config/locales/en.yml
en:
  helpers:
    submit:
      create: "Create %{model}"
      user:
        create: "Sign Up"

设置此选项后,在视图中将产生以下结果:

With this set, in views the following will result:

# /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通知和警报来做到这一点的方法.是否有类似的预设结构用于指定默认即显消息?

So Rails uses a preset hierarchy for getting the submit button text for different models. (i.e., you don't have to tell it which i18n text to get when using f.submit.) I've been trying to find a way to do this with flash notices and alerts. Is there a similar preset structure for specifying default flash messages?

我知道您可以指定自己的任意结构,如下所示:

I know you can specify your own arbitrary structures like the following:

# /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')都很繁琐.有没有一种方法可以使控制器知道"何时抓取并显示在语言环境文件中指定的相应Flash消息?如果是这样,这些的默认YAML结构是什么?

But it's tedious to specify the notice: t('controllers.user_accounts.create.flash.notice') every time. Is there a way to do this so that the controller "just knows" when to grab and display the appropriate flash messages specified in the locale file? If so, what's the default YAML structure for these?

推荐答案

Rails i18n指南第4.1节.关于惰性"查询的第4条提示说:

Rails实现了一种方便的方法来查找 views

(强调他们的意思,至少对我意味着只限于视图...)但是,似乎

(Emphasis theirs, and implying to me, at least, that it is restricted only to views...) However, it seems that this commit to Rails brought "lazy" lookups into controllers as well, with the key being in the form of:

"#{ controller_path.gsub('/', '.') }.#{ action_name }#{ key }"

在您的情况下应该可以帮助您users.create.notice.

which in your case should get you users.create.notice.

因此,如果您对以下内容满意:

So, if you're happy with something like:

# /app/controllers/users_controller.rb
def create
  ...
  redirect_to root_url, notice: t('.notice')
  ...
end

您应该能够在以下位置声明该值:

You should be able to just declare that value in:

# /config/locales/en.yml
en:
  users:
    create:
      notice: "User account was successfully created."

我知道这并不能完全让您拥有默认位置,Rails会在默认位置自动创建并在创建用户失败时获取闪动通知,但这比每次输入完整的i18n密钥要好一些时间.

I know this doesn't take you quite all the way of having a default spot where Rails would automatically go and fetch a flash notice on failure to create a user, but it's a bit better than typing out a full i18n key every time.

这篇关于在Rails中,如何在i18n语言环境文件中指定默认的Flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆