当我试图通过参数locale发出请求时,i18n不改变默认的locale
程序控制器
before_filter :set_locale
respond_to :json
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
我的模型class FoodType < ActiveRecord::Base
default_scope where(locale: I18n.locale)
FoodTypesController
def index
render json: FoodType.all
end
但是在控制台中,查询没有改变。仍在传递先前请求的区域设置
passing locale = pt-BR
FoodType Load (0.4ms) SELECT "food_types".* FROM "food_types" WHERE "food_types"."locale" = 'en'
当FoodType类被加载时,你的作用域被评估。这意味着它将始终具有相同的(初始)值。您需要对像这样的作用域使用lambda,这些作用域取决于一些外部变量(时间是另一个例子):
default_scope, lambda { where(locale: I18n.locale) }