在 Rails 中定义 Content-Language 标头

ber*_*kes 2 ruby-on-rails-4

根据 W3C 的说法,向目标受众传达语言的一种可行且通常首选的方式是提供Content-Language标头。相关部分:

有关目标受众语言的元数据通常最好在文档外部的 HTTP Content-Language 标头中声明。

如何使用 Rails 发送这样的标头?

小智 7

只是比 markshiz 的出色答案更进一步,尝试将其添加到 app/controllers/application_controller.rb 中:

after_action :set_response_language_header
def set_response_language_header
    response.headers["Content-Language"] = I18n.locale.to_s
end
Run Code Online (Sandbox Code Playgroud)

而且您再也不用担心它了。