RubyOnRails - 刷新页面时不保留所选语言

RubyOnRails - Does not keep the selected language when I refresh the page

在开发模式下一切正常。但不是在 heroku 上的生产模式。

当我 select 一个语言环境并刷新页面时,它会默认显示一半时间的语言,否则显示我 select 的语言。我不知道该怎么办。我试图用 Rails.cache.clear 命令清除缓存,但它不起作用。我想问题出在缓存系统上。我是 rails 上 ruby 的新手。有人会知道如何解决这个问题吗?

要了解我的问题,您可以访问我的网站,select 法语并刷新页面几次。一旦页面是法语的。再来一次英语。

https://releaseit.herokuapp.com/

我的application_controller:

  before_action :set_locale
  def set_locale
    if params[:locale].in? %W(en fr)
      I18n.locale = params[:locale]
    end
  end

配置文件与此处相同:https://github.com/starterkits/rails4-starterkit/tree/master/config

对不起我的英语(我是法国人,我使用 google 翻译器)

我不知道(或看不到)您在 URL 中在哪里传递区域设置。据我所知,为了使用 params[:locale] 你的 URL 应该是这样的:

https://releaseit.herokuapp.com/fr/something
https://releaseit.herokuapp.com/en/something
https://releaseit.herokuapp.com/en
https://releaseit.herokuapp.com?locale=fr

http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params

还有什么可以试试我的set_locale方法:

  def set_locale    
    if params[:locale]
      I18n.locale = params[:locale] || I18n.default_locale
    else
      I18n.locale = http_accept_language.compatible_language_from(
        I18n.available_locales
      )
    end
  end

set_locale 方法应该在 每次 页面重新加载时执行,以便每次都设置正确的语言环境。

需要 http_accept_language gem 来自:https://github.com/iain/http_accept_language

另请查看 route_translator gem 以创建具有区域设置的路由。