0

I have a form object

app/form_models/car_registration/basics.rb

module CarRegistration
  class Basics < Base
     include ActiveModel::Model
     delegate :name, :name=, to: :vehicle
  end
end

app/views/car_registration/basics.html.erb

 <%= simple_form_for( @form_object, url: wizard_path, :method => :put) do |f| %>  
    <%= f.input :name %>
    <%= f.submit %>
<% end %>

I could not figure it out by referencing this:

https://guides.rubyonrails.org/i18n.html

As I created the below file, but it does not seem to change my form label.

config/locales/form_models/car_registration/basics/en.yml

en.yml

en:
  name: English Name

simple_form.en.yml

en:
  simple_form:
     car_registration/basics:
        label:
          name: english Name
1

If the form base object is an ActiveModel one, the yaml namespace to override names derived from the objects and attributes would be like this:

en:
  activemodel:
    models:
      car_registration/basics:
        one: BasicsSingular
        other: BasicsPlural
    attributes:
      car_registration/basics:
        name: NameAttributeTranslation

If they are ActiveRecord objects, replace activemodel with activerecord in the yaml file.

  • It's a form object, using Active Model, and it is using Simple_form. any ideas? – user2012677 Mar 15 at 20:20
  • @user2012677 Yes. The answer above should work for you then, are you having trouble with it? – Unixmonkey Mar 15 at 20:56
  • why is car_registration/basics: listed twice?, No it does not work. – user2012677 Mar 15 at 20:58
  • @user2012677 The listing under attributes is to control the labels for attributes of the car_registration/basics "model". The one/other keys under models controls how it might display the model name itself. With the form setup as above, by default <%= f.submit %> would be labeled as "Create Basics", but because I added singular/plural options under models, it would say "Create BasicsSingular". I thought maybe you'd like to know how to override/translate both the name field and the button. – Unixmonkey Mar 15 at 21:07
  • @user2012677 I made a full reproduction of this in a simple app, and what I said is working for me, but I think I found a way to also make it work with your simple_form.en.yml. I think your nested yaml file directory structure isn't being loaded by Rails. Add this to config/application.rb: config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')] to load all sub-directories of config/locales as shown in the Rails I18n guide here: guides.rubyonrails.org/i18n.html#organization-of-locale-files – Unixmonkey Mar 16 at 14:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.