Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

i18n labels for nested models using simple_form

If a project:

  • has_many :tasks
  • accepts_nested_attributes_for :tasks

and I am using simple_form like so (simplified):

simple_form_for @project do |f|
  f.input :project_name
  f.simple_fields_for :tasks do |j|
    j.input :task_name
  end
  f.submit
end

How would I internationalize the label for :task_name? I have tried quite a few combinations in my simple_form.it.yml file, such as:

it:
  simple_form:
    labels:
      project:
        project_name: 'Nome progetto'
        task:
          task_name:  'Nome compito'

Haven't been able to find examples in the docs. Google points out a couple of apparently relevant closed issues:

https://github.com/plataformatec/simple_form/issues/48

https://github.com/plataformatec/simple_form/issues/194

But so far I am at loss...

THANKS! Giuseppe

like image 647
Giuseppe Avatar asked Oct 06 '11 07:10

Giuseppe


3 Answers

As far as your form accepts many tasks you should pluralize task. It works in simple_form:

it:
  simple_form:
    labels:
      project:
        project_name: 'Nome progetto'
        tasks:
          task_name:  'Nome compito'
like image 73
Voldy Avatar answered Sep 20 '22 07:09

Voldy


Use the default Rails i18n.

it:
  activerecord:
    attributes:
      task:
        task_name:  'Nome compito'
like image 25
rafaelfranca Avatar answered Sep 21 '22 07:09

rafaelfranca


SimpleForm should fall back to it.simple_form.labels.task.task_name but it don't.

You can simulate it with some YAML 'hack'.

it:
  simple_form:
    labels:
      task: &task_labels
        task_name:  'Nome compito'
      project:
        project_name: 'Nome progetto'
        tasks:
          <<: *task_labels

You can even customize the labels of project's tasks, after the <<: *task_labels line.

like image 21
Reinaldo Junior Avatar answered Sep 23 '22 07:09

Reinaldo Junior