Rails i18n 前端、后端的不同语言环境

Rails i18n different locales for front-end, back-end

我正在尝试开发具有后端和前端的商务解决方案。我刚刚完成由名称空间分隔的后端,并意识到我需要为每一侧分隔不同的语言环境。那么有没有办法分别设置前后端的locale呢?谢谢指教

http://guides.rubyonrails.org/i18n.html#setting-and-passing-a-locale

The locale can be either set pseudo-globally to I18n.locale (which uses Thread.current like, e.g., Time.zone) or can be passed as an option to #translate and #localize.

If no locale is passed, I18n.locale is used:

I18n.locale = :de
I18n.t :foo
I18n.l Time.now

Explicitly passing a locale:

I18n.t :foo, locale: :de
I18n.l Time.now, locale: :de

The I18n.locale defaults to I18n.default_locale which defaults to :en. The default locale can be set like this: I18n.default_locale = :de

所以,现实生活中的例子(语言环境,基于 Accept-Language HTTP-header (https://github.com/iain/http_accept_language) ):

class ApplicationController < ActionController::Base
  #...
  before_filter :set_locale

  def set_locale
    I18n.locale =     http_accept_language.compatible_language_from(I18n.available_locales)
  end

end