【问题标题】:Can't change locale for rails app when running in Heroku works well in development在 Heroku 中运行时无法更改 Rails 应用程序的语言环境在开发中运行良好
【发布时间】:2011-05-03 15:01:32
【问题描述】:

我正在开发一个双语应用程序。我可以通过传递 ?locale=en 在开发中将语言环境更改为英语,它在开发中有效,但在 heroku 中无效。

通过我在下面插入的记录器,我可以知道语言环境实际上发生了变化,但所有内容都是在默认语言环境中发出的

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_locale

  def set_locale    
    if %w(en pt-BR).include? params[:locale]
      I18n.locale = params[:locale].to_sym
    end
    logger.info I18n.locale
  end
end

config/application.rb

config.i18n.default_locale = :'pt-BR'
config.i18n.locale = :'pt-BR'

【问题讨论】:

    标签: ruby-on-rails internationalization heroku


    【解决方案1】:

    试试这个

    class ApplicationController < ActionController::Base
      protect_from_forgery
      before_filter :set_locale
    
      protected    
      def set_locale
        if params[:locale].blank?
          I18n.locale = :'pt-BR'
        else
          I18n.locale = params[:locale]
        end
      end   
    
      # ensure locale persists
      def default_url_options(options={})
        {:locale => I18n.locale}
      end
    end
    

    在 routes.rb 中

      scope "(:locale)", :locale => /pt-BR|en/ do
        resources :products  # update this!
      end
    

    domain.tld/:locale/ 类型的路由也感觉更干净。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 2020-11-14
      相关资源
      最近更新 更多