【发布时间】:2016-10-27 11:54:16
【问题描述】:
我希望能够在整个 Rails 应用程序中使用全局可访问的符号,以便打印或返回一组易于维护的字符串/句子。我之前听说过一种使用 YAML 的方法,但我不记得它的实现细节或它的名称。
我理想中的一个例子:
控制器
def foo
if token.expired?
render json: { message: :token_expiry_message }
end
end
def bar
if !user.authenticate
flash[:notice] = :token_expiry_message
end
end
Yaml 文件某处
token_expiry_message: "The user token is expired, please re-authenticate"
通过这种方式,我可以通过从 YAML 文件中引用控制器代码并在整个应用程序中使用标准语言集。
【问题讨论】:
-
只使用位于 config/locales 中的语言环境文件。您可以在 application.rb - config.i18n.default_locale = :en 中设置默认语言环境,这将映射到 config/locales 中的 en.yml
标签: ruby-on-rails yaml