How to generate form labels i18n entries

Hi,

I’m surprised that when generating a scaffold, no locale entries are generated. So now I’d like easily generate those entries for my existing forms.

I can’t find any rails tasks, gems for doing this. i18n-tasks doesn’t work on this case.

How do you generate those locale entries ?

Let’s say you want english and portuguese.

Ensure you have this file config/initializers/locale.rb with something like this:

I18n.available_locales = [:en, :pt]
I18n.default_locale = :pt

Create these files:

config/locales/en.yml
config/locales/pt.yml

In each one create this structure:

pt:
  activerecord:
    attributes:
      user:
        name: "Nome"
        last_name: "Sobrenome"

Thank you, I’ve got this part.

I have an application with multiple existing forms, I don’t want to create the structure myself. I’m looking for a way to generate it.

Here’s the generator for I18n YAML files GitHub - amatsuda/i18n_generators: A pack of Rails generators gem plugin that generates Rails 3 and Rails 2 I18n locale files for almost every known locale..

It’s a very old gem that was created 13 years ago for Rails 2.2, but it should still work for recent versions. Just bundle this and run rails g i18n fr, then it will generate the thing that you want (and maybe a little bit more extra things that you didn’t expect).

1 Like

Wow thank you @Akira_Matsuda that’s what I’m looking for.

I’m a bit surprised that the rails generator doesn’t generate those entries. On every new rails project I have to lookup the documentation to create the entries.

1 Like

I, for one, am glad that it does not generate the entries automatically. At best it would generate clutter that I usually don’t need.

To riff on Breno’s example, the auto-generated locale for user might be:

en:
  activerecord:
    attributes:
      user:
        name: "Name"
        last_name: "Last name"

In most cases, it would be redundant because I try to choose attribute names that are meaningful. In the case where the name needs to be localized (because I chose a bad name or I because need to localize immediately to multiple languages), I’d still need to edit the file. Generating the entry doesn’t save me very much.

I’d rather have a helper that generates all the stub entries on demand at the moment that I am ready to localize.

Thanks for sharing this information. It was very useful.