2014-10-09 45 views
0

根據W3C的說法,communicate the language of the intended audience的一個可行且常用的首選方法是提供一個Content-Language頭文件。相關部分:在Rails中定義Content-Language頭文件

關於目標受衆語言的元數據通常最好在HTTP Content-Language頭文件之外的文檔中聲明。

我該如何使用Rails發送這樣的頭文件?

+1

您可以在控制器中使用以下代碼:''response.headers [「Content-Language」] ='en'''。在Rails 4中還有一些新的默認標頭設置: http://chriszetter.com/blog/2013/10/04/setting-headers-in-rails-4/ – 2014-10-09 21:22:38

回答

2

它很可能是首選,你使用這個的I18n框架:

response.headers["Content-Language"] = I18n.locale.to_s 

在這裏閱讀更多:http://guides.rubyonrails.org/i18n.html

+0

確實我打算使用I18n框架。但是,我無法看到我可以從哪裏或如何發送此類標題。控制器中是否有「響應」? – berkes 2014-10-09 21:57:22

+0

@berkes請參閱:「在每個控制器中,有兩個指向請求的訪問方法和與當前正在執行的請求週期相關聯的響應對象。」:http://guides.rubyonrails.org/action_controller_overview.html#響應對象 – markshiz 2014-10-09 22:05:01

+0

在這種情況下:它不應該是「response.headers」嗎? (複數) – berkes 2014-10-10 08:36:53

1

只是前進了一大步過去markshiz的出色答卷,嘗試添加這對應用程序/ controllers/application_controller.rb:

after_action :set_response_language_header 
def set_response_language_header 
    response.headers["Content-Language"] = I18n.locale.to_s 
end 

而且你再也不用擔心它了。