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:
GitHub - paulschreiber/validates_word_count: rails validation plugin. validates word count for a block of text.

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

On Nov 22, 9:53pm, Walter McGinnis [email protected] wrote:

Here’s some example code from a Rails engine:

external_search_sources/init.rb at master · kete/external_search_sources · GitHub

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

Hope that helps,

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

Hi,

Here’s some example code from a Rails engine:

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

Hope that helps,
Walter

Success.

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

Cheers,
Walter