Submit button helper with I18n.t

前端 未结 3 755
孤街浪徒
孤街浪徒 2020-12-18 05:21

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 :



        
3条回答
  •  情书的邮戳
    2020-12-18 05:44

    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 will take the label you need for the button.

    You can see some more info about it here

提交回复
热议问题