0

我正在制作一个使用 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)

先感谢您!

4

1 回答 1

0

我发现了一个我认为可以使用的 hack。我创建了仅在用户注册时才会出现的导航链接,以将它们指定为特定的I18n.locale,并且我正在使用 Pundit 来限制只有注册用户才能进行编辑。如果他们很聪明,他们可以将 URL 更改为另一个国家,并且他们可以通过这种方式访问​​。所以,不理想也不安全,而是一个开始。

于 2015-03-12T16:21:46.513 回答