How to change the locale through URL?

前端 未结 3 1251
攒了一身酷
攒了一身酷 2020-12-06 19:33

In my bi-lingual Rails 4 application I have a LocalesController like this:

class LocalesController < ApplicationController

  def change_loca         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 20:10

    If you want this behaviour, you are going to have to compare the URL against the session every request. One way you might do it is like this:

    before_filter :check_locale
    def check_locale
      if session[:locale] != params[:locale] #I'm assuming this exists in your routes.rb
        params[:set_locale] = params[:locale] #Generally bad to assign things to params but it's short for the example
        change_locale
      end
    end
    

提交回复
热议问题