How do I internationalize/localize my rails plugin?

I wrote a validates_word_count plugin. I would like to store the error messages in a YAML file so they can be easily translated.

Full source:   https://github.com/paulschreiber/validates_word_count/

My plugin's file layout looks like this: validates_word_count/   init.rb   lib/     validates_word_count.rb     locale/       en.yml

My YAML file looks like this: en:   validates_word_count:     errors:       messages:         too_few_words: "has too few words (minimum is %d words)"         too_many_words: "has too many words (maximum is %d words)”

However, if I call I18n.translate('validates_word_count.errors.messages.too_few_words'), I get this error:   translation missing: en, validates_word_count, errors, messages, too_few_words

How can I set up my plugin / locale so I18n.translate() works?

Paul

Hi,

Here's some example code from a Rails engine:

https://github.com/kete/external_search_sources/blob/master/init.rb

You'll note that it follows the Rails convention of having a config and config/locales directory.

Hope that helps, Walter

Unfortunately, it does not. I tried this layout:

validates_word_count/   init.rb   lib/     validates_word_count.rb   config     locales/       en.yml

And I still get the "translation missing" error.

Paul

Yes, but did you add your locales directory to the load path as in init.rb file I linked to? That part is the missing step.

Using the convention of Rails is not enough, you have to tell Rails to include files in the directory.

Cheers, Walter

Success.

I heard back from Paul that that last bit did the trick.

Cheers, Walter