重命名 ActiveAdmin 中的参数/字段

Rename parameters / field in ActiveAdmin

我在 Rails 测试项目中使用 Rails 4.2.5 和最新版本的 ActiveAdmin。我想重命名 ActiveAdmin 中的一个参数,就像我可以用 the name of the class 做的那样,但我还没有找到一种简单的方法来做到这一点。

让我们假设以下模型:

Employee
    name:string
    role:string

我希望 ActiveAdmin 在所有管理面板中将 "role" 参数显示为 "Cargo"(巴西葡萄牙语中的“role”)。

我目前正在使用以下解决方法:

ActiveAdmin.register Employee, as: "empregado" do

    permit_params :name, :role

    # Rename the desired parameter in /admin/empregados
    index do
        column "Nome", :name
        column "Cargo", :role
        actions
    end

    # Rename the desired parameter in /admin/empregados/new
    form do |f|
         inputs do
            input :name, label: "Nome"
            input :role, label: "Cargo"
        end
        actions
    end

    # Rename the desired parameter in /admin/empregados/[:id]
    show do
        attributes_table do
            row :id
            row("Nome"){ empregado.name }
            row("Cargo"){ empregado.role }
            row("Criado em"){ empregado.created_at }
            row("Atualizado em"){ empregado.updated_at }
        end
    end
end

这行得通,但它非常手动,我不认为这是 "Railsistic way" 做事的方式。此外,还有一些模型未重命名的地方,例如用于搜索的 "filters":

我想找到一种无需复制代码即可在所有管理面板中重命名此列/参数的简单方法。有办法吗?

ActiveAdmin 在显示与模型相关的任何内容时使用 ActiveRecord 的翻译,因此在您的情况下,您可以将类似这样的内容添加到您的 es.yml 文件中:

es:
  activerecord:
    models:
      employee:
        one: Empregado
    attributes:
      employee:
        name: Nome
        role: Cargo
        created_at: Criado em
        updated_at: Atualizado em