使用 I18n.t 提交按钮助手 [英] Submit button helper with I18n.t

查看:50
本文介绍了使用 I18n.t 提交按钮助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为提交按钮编写一个助手,它考虑到操作(创建或更新)以获得正确的翻译.他们在这里:

I want to write a helper for a submit button, that takes in account the action (create or update) to get the right translation. Here they are :

fr: 
  submit:
    create:
      user: "Créer mon compte"
      product: "Déposer l'objet"
      session: "Se connecter"
    update:
      user: "Mettre à jour mon compte"
      product: "Modifier l'objet"

我试过了:

def submit_button(model)
  if model == nil
    I18n.t('submit.create.%{model}')
  else
    I18n.t('submit.update.%{model}')
  end
end

但它没有用,rspec 给我发送了:

But it didn't worked and rspec send me that :

Capybara::ElementNotFound: Unable to find button ...

我知道这是一个语法问题,但我不知道如何使它起作用...

I know that's a syntactical problem, but I don't find how to make this work...

推荐答案

你不需要一个助手,你可以用普通的 rails 来实现它.您唯一需要做的就是正确订购您的 I18n YAML

You don't need a helper for that, you can achieve it with plain rails. The only thing you need is to properly order your I18n YAML

fr:
  helpers:
    submit:
      # This will be the default ones, will take effect if no other
      # are specifically defined for the models.
      create: "Créer %{model}"
      update: "Modifier %{model}"

      # Those will however take effect for all the other models below
      # for which we define a specific label.
      user:
        create: "Créer mon compte"
        update: "Mettre à jour mon compte"
      product:
        create: "Déposer l'objet"
        update: "Modifier l'objet"
      session:
        create: "Se connecter"

之后,你只需要像这样定义你的提交按钮:

After that, you only need to define your submit button like this:

<%= f.submit class: 'any class you want to apply' %>

Rails 将采用按钮所需的标签.

Rails will take the label you need for the button.

你可以看到更多关于它的信息 此处

You can see some more info about it here

这篇关于使用 I18n.t 提交按钮助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆