Ruby on rails 如何为注册用户指定I18n语言环境
我正在制作一个应用程序,它使用globalize来翻译数据库。我需要能够为管理员创建一个UI,使其只能够访问特定的地区,以便他们能够翻译这些地区 我想有两种方法可以做到这一点。1-使用Pundit(见下文),或2-管理员登录后,我指定他们的区域设置(不确定如何做到这一点)。我愿意接受其他建议 建议1 我开始使用Pundit,因为我认为我可以使用范围来指定区域设置以进行限制。我需要有三个级别的角色。超级用户可以定义所有语言的范围。“欧洲”用户可以访问欧洲国家的所有语言。特定国家/地区的用户只能为其国家/地区进行翻译Ruby on rails 如何为注册用户指定I18n语言环境,ruby-on-rails,ruby-on-rails-4,rails-i18n,pundit,globalize,Ruby On Rails,Ruby On Rails 4,Rails I18n,Pundit,Globalize,我正在制作一个应用程序,它使用globalize来翻译数据库。我需要能够为管理员创建一个UI,使其只能够访问特定的地区,以便他们能够翻译这些地区 我想有两种方法可以做到这一点。1-使用Pundit(见下文),或2-管理员登录后,我指定他们的区域设置(不确定如何做到这一点)。我愿意接受其他建议 建议1 我开始使用Pundit,因为我认为我可以使用范围来指定区域设置以进行限制。我需要有三个级别的角色。超级用户可以定义所有语言的范围。“欧洲”用户可以访问欧洲国家的所有语言。特定国家/地区的用户只能为其
class IndustryArticlePolicy
attr_reader :user, :model
def initialize(user, model)
@user = user
@industry_article = model
end
class Scope < Struct.new(:user, :scope)
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def scope
if user.super_user?
scope.all
elsif user.europe?
#see method below - don't think it works
scope.where(I18n.locale.europe)
elsif user.role.present?
#dont think below works either
scope.where(role: @user.role.to_sym == I18n.locale)
else
scope.none
end
end
end
def index
@user.super_user? || @user.role.exists?
end
def europe
I18n.locale = :ru
end
end
class行业政策
属性读取器:用户,:模型
def初始化(用户、型号)
@用户=用户
@行业文章=模型
结束
类范围
建议2
(正在尝试找出如何为注册用户指定I18n)
提前谢谢你 我找到了一个我认为可以使用的黑客。我创建了导航链接,仅当用户注册以将其指定到特定的
I18n.locale
时才会出现,并且我使用Pundit来限制只有注册用户才能进行编辑。如果他们聪明的话,他们可以把URL改到另一个国家,这样他们就可以访问。所以,不理想也不安全,只是一个开始