如何为注册用户指定I18n语言环境

时间:2015-03-12 14:35:45

标签: ruby-on-rails ruby-on-rails-4 rails-i18n pundit globalize

我正在创建一个使用globalize来翻译数据库的应用程序。我需要能够为管理员创建一个用户界面只能访问特定区域设置,以便他们可以翻译

我在想两种方法。 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

建议2

(试图找出如何为注册用户指定I18n)

提前谢谢!

1 个答案:

答案 0 :(得分:0)

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

相关问题