感謝大家的幫助。Rails I18n:從HTTP_ACCEPT_LANGUAGE設置初始語言環境
我在做什麼: 我設計一個企業網站,必須在多語言
我做了什麼可用: Rails應用程序與幾個靜態頁面。我曾經在軌使用國際化功能瑞安貝茨例子,特別是在使用路由設置語言環境:
MyApp::Application.routes.draw do
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
match '*path', to: redirect("/#{I18n.locale}/%{path}")
match '', to: redirect("/#{I18n.locale}")
我的ApplicationController如下所示:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options = {})
{locale: I18n.locale}
end
end
我的日誌在服務器啓動顯示以下內容:
Started GET "/en" for 127.0.0.1 at 2012-11-14 22:41:01 +0400
Processing by StaticPagesController#home as HTML
Parameters: {"locale"=>"en"}
Rendered static_pages/home.en.html.erb within layouts/application (1.0ms)
Compiled custom.css (5905ms) (pid 9864)
Compiled application.css (15ms) (pid 9864)
Rendered layouts/_shim.html.erb (0.0ms)
Rendered layouts/_header.html.erb (12.0ms)
Rendered layouts/_footer.html.erb (2.0ms)
Completed 200 OK in 6741ms (Views: 6740.4ms | ActiveRecord: 0.0ms)
我所試圖做的事: 我想,當用戶沒有指定的LO在網址中加入日曆(例如只有www.example.com類型),rails從request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
獲得接受的語言環境,並將訪問者重定向到他的首選語言。在這一點上,我無法弄清楚我的I18n.locale設置在哪裏。
這個問題有沒有任何答案。我有一半 - 但是,我的應用程序控制器中的set_locale之前,語言環境會被設置.... –