如何国际化/本地化我的 Rails 插件?
我编写了一个 validates_word_count 插件。我想将错误消息存储在 YAML 文件中,以便可以轻松翻译它们。
我的插件的文件布局如下所示:
validates_word_count/
init.rb
lib/
validates_word_count.rb
locale/
en.yml
我的 YAML 文件如下所示:
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)"
但是,如果我调用 I18n.translate('validates_word_count.errors.messages.too_few_words')
,我会收到此错误:
translation missing: en, validates_word_count, errors, messages, too_few_words
我该如何设置我的插件/语言环境以便 I18n.translate() 有效吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案分为两部分。
1.使用标准目录布局:
2。在 init.rb 中,添加以下行:
There are two parts to the answer.
1. Use the standard directory layout:
2. In init.rb, add the following lines: