我正在制作一个使用 globalize 来翻译数据库的应用程序。我需要能够为管理员创建一个 UI,以便他们只能访问特定的语言环境,以便他们可以翻译它们。
我正在考虑两种方法来做到这一点。1- 使用 Pundit(见下文),或 2- 管理员登录后,我指定他们的语言环境(不知道如何执行此操作)。我愿意接受其他建议。
建议一
我开始使用 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
建议二
(试图弄清楚如何为注册用户指定 I18n)
先感谢您!