感谢大家的帮助。
我在做什么: 我正在设计一个必须以多种语言提供的公司网站
我做了什么: 具有多个静态页面的rails应用程序。我使用Ryan Bates示例在rails中使用I18n功能,特别是使用路由来设置语言环境:
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)
我在做什么:
我希望当用户没有在网址中指定区域设置时(例如仅限类型为www.example.com),rails会从request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
获取接受的区域设置
并将访问者重定向到他的首选语言。在这一点上,我无法弄清楚我的I18n.locale首先在哪里设置。
答案 0 :(得分:0)
答案 1 :(得分:0)